[异常] Asp.net 中异常处理

异常主要有 应用程序异常和公共语言库异常。

比如: DirectoryNotFoundException  不存在文件夹

        FileNotFoundException  文件没找到异常

       InvalidCastException 类型错误异常

通用结构

try{}

catch(Exception e){}

finally{}

注意:

1.finally 中的代码是必执行的,即使 catch 代码段中有 return字样。

2. 多个catch,只会捕捉最上面的异常,只要捕捉到一个异常,后面的异常就不会捕捉了。所以最上面应该为最具体的异常。

3. 当多个try 嵌套后, 里面捕捉的异常后,外面的异常不会捕捉到。

代码抛出异常

throw  new FileNotFoundException(“文件没找到”);

自定义异常累  customizeException     定义3个构造函数即可。

class customize1Exception:Exception
{
public customize1Exception()
{

}
public customize1Exception(string message)
:base(message)
{

}
public customize1Exception(string message, Exception inner)
: base(message, inner)
{

}
}

抛出自定义异常throw new customizeException(“文件没找到”);

原文地址:https://www.cnblogs.com/StudyLife/p/3010592.html