C#异常

2017-09-18  本文已影响13人  晓龙酱

使用原则:

在try finally中返回数据

异常嵌套

// Wrong
try
{
    Method1();
}
<font color=red>catch(Exception err)</font>
{
    // 这里没有处理错误,而直接继续转发这个错误,会导致这之前堆栈信息丢失
    <font color=red>throw err</font>;
}
// correct
try
{
    Method1();
}
<font color=red>cache</font>
{
    <font color=red>throw;</font>
}

捕获未处理的异常

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

static void CurrentDomain_UnhandleException(object sender, UnhandledExceptionEventArgs e)
{
}

使用自定义异常

上一篇 下一篇

猜你喜欢

热点阅读