« Exception » : différence entre les versions
Apparence
Ligne 19 : | Ligne 19 : | ||
= Argument Exception = | = Argument Exception = | ||
<kode lang='cs'> | <kode lang='cs'> | ||
public | public void MyMethod(string argument1) | ||
{ | { | ||
ArgumentException.ThrowIfNullOrEmpty(argument1, nameof(argument1)); | ArgumentException.ThrowIfNullOrEmpty(argument1, nameof(argument1)); |
Version du 16 avril 2024 à 12:56
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));
}
|
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();
}
|