이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

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`.

Learn More

Non-Compliant Code Examples

using System.Xml;

class MyClass {
    public void myMethod()
    {
        var a = new MyException("something bad happened");
    }
}

Compliant Code Examples

using System.Xml;

class MyClass {
    public void myMethod()
    {
        var bla = new MyClass("something bad happened");
    }
    public void foo() {
            Formatter formatter = language switch
            {
                SqlLanguage.Sql => new StandardSqlFormatter(),
                SqlLanguage.Tsql => new TSqlFormatter(),
                SqlLanguage.Spark => new SparkSqlFormatter(),
                SqlLanguage.RedShift => new RedshiftFormatter(),
                SqlLanguage.PostgreSql => new PostgreSqlFormatter(),
                SqlLanguage.PlSql => new PlSqlFormatter(),
                SqlLanguage.N1ql => new N1qlFormatter(),
                SqlLanguage.MySql => new MySqlFormatter(),
                SqlLanguage.MariaDb => new MariaDbFormatter(),
                SqlLanguage.Db2 => new Db2Formatter(),
                _ => throw new NotSupportedException(),
            };
    }
}
using System.Xml;

class MyClass {
    public void myMethod()
    {
        throw new MyException("something bad happened");
    }
}
PREVIEWING: brett.blue/embedded-collector-release