C#各種異常處理方式總結(jié)
.NET的異常處理機(jī)制用來發(fā)現(xiàn)、處理運(yùn)行時(shí)錯(cuò)誤。如果開發(fā)人員沒有提供異常的處理機(jī)制,就默認(rèn)采用.NET的機(jī)制。
通常使用try...catch...finally捕獲異常。
try
{
//有可能發(fā)生異常
}
catch(Exception ex)
{
//處理異常
}
finally
{
//清理
}- 如果沒有異常發(fā)生,就直接到finally語句塊中。
- finally語句塊是必須執(zhí)行的
- 這里的catch和finally語句塊是可選的。try語句塊后面可以跟1個(gè)或多個(gè)catch語句塊,try語句塊后面可以直接跟finally語句塊。
- Exception是所有異常的基類
使用.NET默認(rèn)異常處理機(jī)制捕獲異常
class Program
{
static void Main(string[] args)
{
int a = 0;
int result = 100/a;
Console.WriteLine(result);
Console.ReadKey();
}
}
使用try...catch手動(dòng)捕獲異常
class Program
{
static void Main(string[] args)
{
int a = 0;
int result = 0;
try
{
result = 100/a;
Console.WriteLine("這里不會(huì)執(zhí)行");
}
catch (DivideByZeroException exception)
{
Console.WriteLine("出現(xiàn)異常");
}
Console.WriteLine(result);
Console.ReadKey();
}
}
使用try...catch...finally手動(dòng)捕獲異常
class Program
{
static void Main(string[] args)
{
int a = 0;
int result = 0;
try
{
result = 100/a;
Console.WriteLine("這里不會(huì)執(zhí)行");
}
catch (DivideByZeroException exception)
{
Console.WriteLine("出現(xiàn)異常");
}
finally
{
Console.WriteLine("放行吧,肯定會(huì)執(zhí)行到我這里的~~");
}
Console.WriteLine(result);
Console.ReadKey();
}
}
可見,finally語句塊中的內(nèi)容一定會(huì)被執(zhí)行。
使用try...多個(gè)catch...finally手動(dòng)捕獲異常
class Program
{
static void Main(string[] args)
{
int a = 0;
int result = 0;
try
{
result = 100/a;
Console.WriteLine("這里不會(huì)執(zhí)行");
}
catch (DivideByZeroException exception)
{
Console.WriteLine("不能被0除的異常");
}
catch (Exception ex)
{
Console.WriteLine("異常");
}
finally
{
Console.WriteLine("放行吧,肯定會(huì)執(zhí)行到我這里的~~");
}
Console.WriteLine(result);
Console.ReadKey();
}
}
可見,只要有一個(gè)catch語句塊捕獲到異常,其它c(diǎn)atch語句塊不執(zhí)行。
使用try...catch(不帶括號(hào),不帶參數(shù))手動(dòng)捕獲異常
class Program
{
static void Main(string[] args)
{
int a = 0;
int result = 0;
try
{
result = 100/a;
Console.WriteLine("這里不會(huì)執(zhí)行");
}
catch
{
Console.WriteLine("異常");
}
Console.WriteLine(result);
Console.ReadKey();
}
}
通過以上方法,可以捕獲任何異常。
try...catch手動(dòng)捕獲拋出的異常
class Program
{
static void Main(string[] args)
{
try
{
throw new DivideByZeroException("除數(shù)不能為零");
}
catch (DivideByZeroException e)
{
Console.WriteLine("異常");
}
Console.WriteLine("最后想說的");
Console.ReadKey();
}
}
拋出異常本身并沒有顯示。
較高層次上下文捕獲較低拋出的異常
class Program
{
static void Main(string[] args)
{
Calculate c = new Calculate();
try
{
c.Divide();
}
catch (Exception e)
{
Console.WriteLine("捕獲異常");
}
Console.WriteLine("最后想說的");
Console.ReadKey();
}
}
public class Calculate
{
public void Divide()
{
try
{
int a = 0;
int result = 100/a;
}
catch (DivideByZeroException e)
{
throw;
}
}
}
在Calculate內(nèi)部拋出的異常,被更高層次的客戶端捕獲。
自定義異常
class Program
{
static void Main(string[] args)
{
try
{
throw new MyException("i am exception");
}
catch (Exception e)
{
Console.WriteLine("捕獲到自定義異常了~~");
}
Console.WriteLine("最后想說的");
Console.ReadKey();
}
}
public class MyException : Exception
{
public MyException(string str)
{
Console.WriteLine("這是我自定義的異常:" + str);
}
}
總結(jié):
- .NET異常處理并不是標(biāo)準(zhǔn)的try...catch...finally,可以是很靈活的。
- 盡量在較低層次拋異常,在較高層次捕獲異常。
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C#在驗(yàn)證文件共享模式下實(shí)現(xiàn)多線程文件寫入
這篇文章主要為大家詳細(xì)介紹了C#在驗(yàn)證文件共享模式下實(shí)現(xiàn)多線程文件寫入的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-01-01
c#使用Dataset讀取XML文件動(dòng)態(tài)生成菜單的方法
這篇文章主要介紹了c#使用Dataset讀取XML文件動(dòng)態(tài)生成菜單的方法,涉及C#使用Dataset操作XML文件的相關(guān)技巧,需要的朋友可以參考下2015-05-05
C#小程序15位轉(zhuǎn)18位身份證號(hào)代碼
現(xiàn)在我們使用的都是18位身份證號(hào),而以前都是15位身份證號(hào),而如何將15位身份證號(hào)轉(zhuǎn)18位身份證號(hào)轉(zhuǎn)換為18位身份證號(hào)呢?2013-02-02
C#實(shí)現(xiàn)將Excel表格轉(zhuǎn)換為圖片(JPG/?PNG)
Excel表格可能會(huì)因?yàn)椴煌O(shè)備或字體缺失等問題,導(dǎo)致格式錯(cuò)亂或數(shù)據(jù)顯示異常,轉(zhuǎn)換為圖片后,能確保數(shù)據(jù)的排版等保持一致,下面我們看看如何使用C#實(shí)現(xiàn)將Excel表格轉(zhuǎn)換為圖片吧2025-04-04

