C# 如何知道捕获哪个特定异常

C# 如何知道捕获哪个特定异常,c#,.net,exception,C#,.net,Exception,比如说 try { Application.Exit(); } catch (Exception ex) { MessageBox.Show(ex.Message); //throw; } 这是一般的例外。如何知道使用哪一个以及何时使用 如何知道使用哪一种以及何时使用 这取决于您在try块中尝试执行的操作。假设您正在使用SqlCommand检索一些记录,那么最好先捕获SqlException,然后再捕获其他记录 try { using(SqlCommand cm

比如说

try
{
    Application.Exit();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
    //throw;
}
这是一般的例外。如何知道使用哪一个以及何时使用

如何知道使用哪一种以及何时使用

这取决于您在
try
块中尝试执行的操作。假设您正在使用
SqlCommand
检索一些记录,那么最好先捕获
SqlException
,然后再捕获其他记录

try
{
    using(SqlCommand cmd = new SqlCommand(....))
    {
        //........
    }
}
catch (SqlException se)
{
    //logging etc. 
}
catch (Exception ex)
{
    //logging etc
}
只需记住首先捕获特定的异常,然后移动到base
Exception
。至于您关于捕获哪个异常的问题,您无法确定它们,这就是为什么它们被称为异常,您只能相应地预测和捕获

如何知道使用哪一种以及何时使用

这取决于您在
try
块中尝试执行的操作。假设您正在使用
SqlCommand
检索一些记录,那么最好先捕获
SqlException
,然后再捕获其他记录

try
{
    using(SqlCommand cmd = new SqlCommand(....))
    {
        //........
    }
}
catch (SqlException se)
{
    //logging etc. 
}
catch (Exception ex)
{
    //logging etc
}

只需记住首先捕获特定的异常,然后移动到base
Exception
。至于您关于捕获哪个异常的问题,您无法确定它们,这就是为什么它们被称为异常,您只能相应地预测和捕获

您可以定义许多捕捉块。比如:

        try
        {
            var result = 8 / 0;
        }
        catch (DivideByZeroException ex)
        {
            // Here, a more specific exception is caught.
        }
        catch (Exception ex)
        {
           // Otherwise, you'll get the exception here.
        }

可以定义许多捕捉块。比如:

        try
        {
            var result = 8 / 0;
        }
        catch (DivideByZeroException ex)
        {
            // Here, a more specific exception is caught.
        }
        catch (Exception ex)
        {
           // Otherwise, you'll get the exception here.
        }

您希望在程序中使用结构化异常处理(SEH)

首先,您必须注意,存在3种类型的错误:

  • 程序错误
  • 用户错误
  • 例外情况
所以,您必须在C#中使用SEH,这只是在可以创建异常的情况下,但在程序运行期间,您不能对代码执行任何操作。在创建方法的过程中,若要避免方法无法执行请求的任务时的情况,必须将该方法设置为异常。在这个过程中,你必须注意两点(正如里赫特在他的一本书中所写的那样):

首先,您必须了解可以创建什么类型的异常。必须非常小心地选择此类型,为此,您可以使用FCL(框架类库)中的现有类型之一,但有时在您需要自己的异常类型时,可能会出现这种情况。例如,如果使用输入Otput类,则可以使用以下异常类型之一-
System.IO.DirectoryNotFoundException、System.IO.DriveNotFoundException、System.IO.EndOfStreamException、System.IO.FileLoadException、System.IO.FileNotFoundException、System.IO.pathtoolongeexception、System.IO.PipeException

其次,您必须选择要发送给异常构造函数的消息。意味着您必须能够从方法中找到详细信息—创建此异常的时间和原因

对于创建异常,也可以使用next shablon

void SomeMEthod (){
try 
{
    //your code to do
}
catch (ExceptionType1) //here put code with Exception 1
{
// you can add some specific code here for working with your exception
}
catch (ExceptionType2) //here put code with Exception 2
{
// you can add some specific code here for working with your exception
}
catch (EXCEPTION) //here put code with rest types of Exception
{
// you can add some specific code here for working with your exception
}
finally
{
//here put code that allow you to free your resourses
//NOTE: this code will be launched always!
}
//here you can placed code, that will be launched if no Exception will be found
}
另外请注意,
EXCEPTION
必须包括
ExceptionType2
ExceptionType1
,并且
ExceptionType2
必须包括
ExceptionType1
-这允许您使用一些层次结构捕获异常-否则-
EXCEPTION
将捕获您的所有异常

此外,您还可以在
catch
块中添加关键字
thow
——用于现有异常的双重工作

catch (EXCEPTION) //here put code with rest types of Exception
{
// you can add some specific code here for working with your exception
throw;
}
此外,您还必须了解,若您使用代码捕获异常,您知道它是可以的。所以你可以做些东西把它拿走。您必须以这样的方式创建代码,即带有
try catch funaly
的q-ty块正是您的程序所需要的


您可以找到有关C中异常类的详细信息。

您希望在程序中使用结构化异常处理(SEH)

首先,您必须注意,存在3种类型的错误:

  • 程序错误
  • 用户错误
  • 例外情况
所以,您必须在C#中使用SEH,这只是在可以创建异常的情况下,但在程序运行期间,您不能对代码执行任何操作。在创建方法的过程中,若要避免方法无法执行请求的任务时的情况,必须将该方法设置为异常。在这个过程中,你必须注意两点(正如里赫特在他的一本书中所写的那样):

首先,您必须了解可以创建什么类型的异常。必须非常小心地选择此类型,为此,您可以使用FCL(框架类库)中的现有类型之一,但有时在您需要自己的异常类型时,可能会出现这种情况。例如,如果使用输入Otput类,则可以使用以下异常类型之一-
System.IO.DirectoryNotFoundException、System.IO.DriveNotFoundException、System.IO.EndOfStreamException、System.IO.FileLoadException、System.IO.FileNotFoundException、System.IO.pathtoolongeexception、System.IO.PipeException

其次,您必须选择要发送给异常构造函数的消息。意味着您必须能够从方法中找到详细信息—创建此异常的时间和原因

对于创建异常,也可以使用next shablon

void SomeMEthod (){
try 
{
    //your code to do
}
catch (ExceptionType1) //here put code with Exception 1
{
// you can add some specific code here for working with your exception
}
catch (ExceptionType2) //here put code with Exception 2
{
// you can add some specific code here for working with your exception
}
catch (EXCEPTION) //here put code with rest types of Exception
{
// you can add some specific code here for working with your exception
}
finally
{
//here put code that allow you to free your resourses
//NOTE: this code will be launched always!
}
//here you can placed code, that will be launched if no Exception will be found
}
另外请注意,
EXCEPTION
必须包括
ExceptionType2
ExceptionType1
,并且
ExceptionType2
必须包括
ExceptionType1
-这允许您使用一些层次结构捕获异常-否则-
EXCEPTION
将捕获您的所有异常

此外,您还可以在
catch
块中添加关键字
thow
——用于现有异常的双重工作

catch (EXCEPTION) //here put code with rest types of Exception
{
// you can add some specific code here for working with your exception
throw;
}
此外,您还必须了解,若您使用代码捕获异常,您知道它是可以的。所以你可以做些东西把它拿走。您必须以这样的方式创建代码,即带有
try catch funaly
的q-ty块正是您的程序所需要的

关于C#you中异常类的详细信息