This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:csharp-best-practices/exception-must-be-thrown
Language: C#
Severity: Warning
Category: Best Practices
Description
Exceptions should be thrown and not just created. An expression such as new Exception(...) does not throw the exception. You should use the keyword throw to throw the exception`.
usingSystem.Xml;classMyClass{publicvoidmyMethod(){vara=newMyException("something bad happened");}}
Compliant Code Examples
usingSystem.Xml;classMyClass{publicvoidmyMethod(){varbla=newMyClass("something bad happened");}publicvoidfoo(){Formatterformatter=languageswitch{SqlLanguage.Sql=>newStandardSqlFormatter(),SqlLanguage.Tsql=>newTSqlFormatter(),SqlLanguage.Spark=>newSparkSqlFormatter(),SqlLanguage.RedShift=>newRedshiftFormatter(),SqlLanguage.PostgreSql=>newPostgreSqlFormatter(),SqlLanguage.PlSql=>newPlSqlFormatter(),SqlLanguage.N1ql=>newN1qlFormatter(),SqlLanguage.MySql=>newMySqlFormatter(),SqlLanguage.MariaDb=>newMariaDbFormatter(),SqlLanguage.Db2=>newDb2Formatter(),_=>thrownewNotSupportedException(),};}}
usingSystem.Xml;classMyClass{publicvoidmyMethod(){thrownewMyException("something bad happened");}}
1
2
rulesets:- csharp-best-practices # Rules to enforce C# best practices.