上一篇:
C#,入门教程(35)——哈希表(Hashtable)的基础知识与用法https://blog.csdn.net/beijinghorn/article/details/124236243
1、try catch 错误机制
Try-catch 语句包含一个后接一个或多个 catch 子句的 try 块,这些子句指定不同异常的处理程序。
引发异常时,公共语言运行时 (CLR) 查找处理此异常的 catch 语句。 如果当前正在执行的方法不包含此类 catch 块,则 CLR 查看调用了当前方法的方法,并以此类推遍历调用堆栈。 如果未找到任何 catch 块,则 CLR 向用户显示一条未处理的异常消息,并停止执行程序。
try 块包含可能导致异常的受保护的代码。
The try catch statement contains a Try block followed by one or more catch clauses that specify different exception handlers.
When an exception is thrown, the common language runtime (CLR) looks for a catch statement that handles the exception. If the currently executing method does not contain such a catch block, the CLR checks the method that called the current method, and so on. If no catch block is found, the CLR displays an unhandled exception message to the user and stops executing the program.
The try block contains protected code that may cause exceptions.
尽管可以不带参数使用 catch 子句来捕获任何类型的异常,但不推荐这种用法。 一般情况下,只应捕获你知道如何从其恢复的异常。 因此,应始终指定派生自 System.Exception 的对象参数。 异常类型应尽可能具体,以避免不正确地接受异常处理程序实际上无法解决的异常。 因此,最好是在基 Exception 类型上使用具体的异常。
可以使用同一 try-catch 语句中的多个特定 catch 子句。 在这种情况下,catch 子句的顺序很重要,因为 catch 子句是按顺序检查的。 在使用更笼统的子句之前获取特定性更强的异常。 如果捕获块的排序使得永不会达到之后的块,则编译器将产生错误。
Although the catch clause can be used without parameters to catch any type of exception, this usage is not recommended. Generally, you should only catch exceptions from which you know how to recover. Therefore, object parameters derived from System.Exception should always be specified. The exception type should be as specific as possible to avoid incorrectly accepting exceptions that the exception handler cannot actually resolve. Therefore, it is better to use specific exceptions on the base Exception type.
You can use multiple specific catch clauses in the same try catch statement. In this case, the order of the catch clauses is important because the catch clauses are checked in order. Get more specific exceptions before using more general clauses. If the order of the captured blocks is such that the subsequent blocks are never reached, the compiler will generate an error.
筛选想要处理的异常的一种方式是使用 catch 参数。 也可以使用异常筛选器进一步检查该异常以决定是否要对其进行处理。 如果异常筛选器返回 false,则继续搜索处理程序。
异常筛选器要优于捕获和重新引发(如下所述),因为筛选器将保留堆栈不受损坏。 如果之后的处理程序转储堆栈,可以查看到异常的原始来源,而不只是重新引发它的最后一个位置。 异常筛选器表达式的一个常见用途是日志记录。 可以创建一个始终返回 false 并输出到日志的筛选器,能在异常通过时进行记录,且无需处理并重新引发它们。
可在 catch 块中使用 throw 语句以重新引发已由 catch 语句捕获的异常。 下面的示例从 IOException 异常提取源信息,然后向父方法引发异常。
One way to filter the exceptions you want to handle is to use the catch parameter. You can also use an exception filter to further examine the exception to determine whether to handle it. If the exception filter returns false, continue searching for handlers.
Exception filters are preferable to capture and rethrow (as described below), because the filter will keep the stack intact. If the subsequent handler dumps the stack, you can see the original source of the exception, not just the last location where it was re thrown. A common use of exception filter expressions is logging. You can create a filter that always returns false and outputs it to the log. It can record exceptions when they pass without processing and rethrowing them.
You can use a throw statement in a catch block to rethrow an exception that has been caught by a catch statement. The following example extracts the source information from the IOException exception, and then throws an exception to the parent method.
2、关于异常的一点细节问题
C# 的错误机制,try catch 除了可以捕捉常规的 Exception 异常,也可以捕捉到 系统定义的 或 自定义的 不同异常,并支持应用程序按不同的异常进行分别的处理。
using System;
using System.IO;
private void MultiExceptions(string filepath)
{
try
{
// 文件的操作
long isize;
FileStream fs = File.Open(filepath, FileMode.Open);
isize = fs.Length;
fs.Close();
}
catch (UnauthorizedAccessException uex)
{
// 未授权的权限导致的异常
MessageBox.Show(uex.Message);
}
catch (FileNotFoundException fex)
{
// 无文件导致的异常
MessageBox.Show(fex.Message);
}
catch (NotSupportedException nex)
{
// 不支持操作的错误导致的异常
MessageBox.Show(nex.Message);
}
catch (ArgumentException aex)
{
// 参数错误导致的异常
MessageBox.Show(aex.Message);
}
catch
{
// 其他错误导致的异常
MessageBox.Show("error occured!");
}
}
下一篇:
C#,入门教程(37)——一个优秀程序员的修炼之道https://blog.csdn.net/beijinghorn/article/details/125011644