C#各種異常處理方式總結(jié)
.NET的異常處理機(jī)制用來發(fā)現(xiàn)、處理運(yùn)行時錯誤。如果開發(fā)人員沒有提供異常的處理機(jī)制,就默認(rèn)采用.NET的機(jī)制。
通常使用try...catch...finally捕獲異常。
try { //有可能發(fā)生異常 } catch(Exception ex) { //處理異常 } finally { //清理 }
- 如果沒有異常發(fā)生,就直接到finally語句塊中。
- finally語句塊是必須執(zhí)行的
- 這里的catch和finally語句塊是可選的。try語句塊后面可以跟1個或多個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手動捕獲異常
class Program { static void Main(string[] args) { int a = 0; int result = 0; try { result = 100/a; Console.WriteLine("這里不會執(zhí)行"); } catch (DivideByZeroException exception) { Console.WriteLine("出現(xiàn)異常"); } Console.WriteLine(result); Console.ReadKey(); } }
使用try...catch...finally手動捕獲異常
class Program { static void Main(string[] args) { int a = 0; int result = 0; try { result = 100/a; Console.WriteLine("這里不會執(zhí)行"); } catch (DivideByZeroException exception) { Console.WriteLine("出現(xiàn)異常"); } finally { Console.WriteLine("放行吧,肯定會執(zhí)行到我這里的~~"); } Console.WriteLine(result); Console.ReadKey(); } }
可見,finally語句塊中的內(nèi)容一定會被執(zhí)行。
使用try...多個catch...finally手動捕獲異常
class Program { static void Main(string[] args) { int a = 0; int result = 0; try { result = 100/a; Console.WriteLine("這里不會執(zhí)行"); } catch (DivideByZeroException exception) { Console.WriteLine("不能被0除的異常"); } catch (Exception ex) { Console.WriteLine("異常"); } finally { Console.WriteLine("放行吧,肯定會執(zhí)行到我這里的~~"); } Console.WriteLine(result); Console.ReadKey(); } }
可見,只要有一個catch語句塊捕獲到異常,其它c(diǎn)atch語句塊不執(zhí)行。
使用try...catch(不帶括號,不帶參數(shù))手動捕獲異常
class Program { static void Main(string[] args) { int a = 0; int result = 0; try { result = 100/a; Console.WriteLine("這里不會執(zhí)行"); } catch { Console.WriteLine("異常"); } Console.WriteLine(result); Console.ReadKey(); } }
通過以上方法,可以捕獲任何異常。
try...catch手動捕獲拋出的異常
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)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
winform模擬鼠標(biāo)按鍵的具體實(shí)現(xiàn)
這篇文章介紹了winform模擬鼠標(biāo)按鍵的具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-10-10C#/VB.NET 實(shí)現(xiàn)在PDF表格中添加條形碼
條碼的應(yīng)用已深入生活和工作的方方面面。在處理?xiàng)l碼時,常需要和各種文檔格式相結(jié)合。本文,以操作PDF文件為例,介紹如何在編輯表格時,向單元格中插入條形碼,需要的可以參考一下2022-06-06詳解C#批量插入數(shù)據(jù)到Sqlserver中的四種方式
本文主要講解一下在Sqlserver中批量插入數(shù)據(jù)。文中大數(shù)據(jù)批量插入方式一和方式四盡量避免使用,而方式二和方式三都是非常高效的批量插入數(shù)據(jù)方式,需要的朋友可以看下2016-12-12C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法
這篇文章主要介紹了C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法,涉及C#針對sql server數(shù)據(jù)庫的簡單查詢技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08