This product is not supported for your selected Datadog site. ().
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 Security
Datadog Code Security
Try this rule and analyze your code with Datadog Code Security
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 Security scans to your CI pipelines