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
}
只需记住首先捕获特定的异常,然后移动到baseException
。至于您关于捕获哪个异常的问题,您无法确定它们,这就是为什么它们被称为异常,您只能相应地预测和捕获
如何知道使用哪一种以及何时使用
这取决于您在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种类型的错误:
- 程序错误
- 用户错误
- 例外情况
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种类型的错误:
- 程序错误
- 用户错误
- 例外情况
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中异常类的详细信息