« Exception » : différence entre les versions
Apparence
(3 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 14 : | Ligne 14 : | ||
throw ex; // relance l'exception, mais la stacktrace est écrasée | throw ex; // relance l'exception, mais la stacktrace est écrasée | ||
throw new CustomException("Error message", ex); // lance une nouvelle exception avec le précédente exception dans inner exception | throw new CustomException("Error message", ex); // lance une nouvelle exception avec le précédente exception dans inner exception | ||
} | |||
</kode> | |||
= Argument Exception = | |||
<kode lang='cs'> | |||
public void MyMethod(string argument1) | |||
{ | |||
ArgumentException.ThrowIfNullOrEmpty(argument1, nameof(argument1)); | |||
ArgumentNullException.ThrowIfNull(argument1, nameof(argument1)); | |||
ArgumentOutOfRangeException.ThrowIfEqual(argument1, "", nameof(argument1)); | |||
ArgumentOutOfRangeException.ThrowIfZero(argument1, nameof(argument1)); | |||
} | } | ||
</kode> | </kode> | ||
Ligne 30 : | Ligne 41 : | ||
} | } | ||
</kode> | </kode> | ||
= [https://learn.microsoft.com/en-us/dotnet/standard/exceptions/how-to-create-user-defined-exceptions User-defined exceptions] = | |||
<filebox fn='MyException.cs'> | |||
public class MyException : Exception | |||
{ | |||
public MyException() | |||
{ | |||
} | |||
public EmployeeListNotFoundException(string message) | |||
: base(message) | |||
{ | |||
} | |||
public EmployeeListNotFoundException(string message, Exception inner) | |||
: base(message, inner) | |||
{ | |||
} | |||
} | |||
</filebox> |
Dernière version du 8 janvier 2025 à 08:57
Liens
Exemple
try { /* ... */ }
catch (InvalidCastException icex) { /* ... */ }
// exception filter, si le test n'est pas valide: relance l'exception sans modifier la stacktrace
catch (Exception ex) when (ex.ParamName == "...") { /* ... */ }
catch (Exception ex) {
throw; // relance l'exception
throw ex; // relance l'exception, mais la stacktrace est écrasée
throw new CustomException("Error message", ex); // lance une nouvelle exception avec le précédente exception dans inner exception
}
|
Argument Exception
public void MyMethod(string argument1)
{
ArgumentException.ThrowIfNullOrEmpty(argument1, nameof(argument1));
ArgumentNullException.ThrowIfNull(argument1, nameof(argument1));
ArgumentOutOfRangeException.ThrowIfEqual(argument1, "", nameof(argument1));
ArgumentOutOfRangeException.ThrowIfZero(argument1, nameof(argument1));
}
|
ExceptionDispatchInfo
Permet de capturer une exception et sa StackTrace pour la relancer ensuite.
catch (Exception ex)
{
ExceptionDispatchInfo capturedException = ExceptionDispatchInfo.Capture(ex);
}
if (capturedException != null)
{
capturedException.Throw();
}
|
User-defined exceptions
MyException.cs |
public class MyException : Exception
{
public MyException()
{
}
public EmployeeListNotFoundException(string message)
: base(message)
{
}
public EmployeeListNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}
|