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");}}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- csharp-best-practices # Rules to enforce C# best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines