1 / 20

第九章

第九章. 例外處理. 9.1 Try…Catch Finally 語法. 遇到執行時期錯誤或不正常狀況,稱為 「例外」。 我們可將這些例外置於 Try..Catch…End Try 中對這些例外做相關的處理。 要尋找其發生原因,再求解決方法。 例如索引值超出陣列允許範圍 … 等,這些即 需用到例外處理。 VB 2005 使用 Try…Catch… Finally 敘述來解決例外處理。其語法如下:. 語法:. Try [tryStatements] [Catch [exception [As type] ] [catchStatements] ]

avari
Download Presentation

第九章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第九章 例外處理

  2. 9.1 Try…Catch Finally語法 • 遇到執行時期錯誤或不正常狀況,稱為「例外」。 • 我們可將這些例外置於 Try..Catch…End Try中對這些例外做相關的處理。 • 要尋找其發生原因,再求解決方法。 • 例如索引值超出陣列允許範圍…等,這些即需用到例外處理。VB 2005使用Try…Catch… Finally 敘述來解決例外處理。其語法如下:

  3. 語法: Try [tryStatements] [Catch [exception [As type] ] [catchStatements] ] [Exit Try] ……… [Finally [FinallyStatements]] End Try

  4. 【說明】 • tryStatements:有可能發生錯誤的敘述區段。 • Catch:允許多個Catch敘述區段,由上至下Catch逐一檢查,當符合例外類別即執行catchStatements敘述區段,而下面Catch條件即不再處理。 • exception:為自取變數名稱,該變數有初始值時即發生錯誤值。 • type:Exception例外類別,用來篩選錯誤的條件類型。 • Exit Try:離開Try…Catch敘述,直接到End Try後面敘述,不適用在有Finally敘述中。 • Finally:接在最後一個Catch之後,不論是否有執行catchStatements都會執行finallyStatements。

  5. 9.2.1 常用例外類別

  6. 9.2.2 例外類別常用成員

  7. 【程式碼】 FileName:TryCatch-1.sln 01 Module Module1 03 Sub Main() 04 Dim a, b, c As Integer 05 Console.Write("a = ") 06 a = Console.ReadLine() 07 Console.Write("b = ") 08 b = Console.ReadLine() 09 c = a \ b 10 Console.WriteLine("a / b = {0}", c) 11 Console.ReadLine() 12 End Sub 14 End Module

  8. 【程式碼】 FileName:TryCatch-2.sln 01 Module Module1 03 Sub Main() 04 Dim a, b, c As Integer 05 Console.Write("a = ") 06 a = Console.ReadLine() 07 Console.Write("b = ") 08 b = Console.ReadLine() 09 Try 10 c = a \ b 11 Catch ex As Exception 12 Console.WriteLine(ex.ToString) 13 End Try 14 Console.WriteLine("a / b = {0}" , c) 15 Console.Read() 16 End Sub 18 End Module

  9. 【程式碼】 FileName:TryCatch-3.sln 01 Module Module1 03 Sub Main() 04 Dim i As Integer 05 Dim score() As Integer = {1, 2, 3} 06 For i = 1 To 3 07 Console.Write("score({0})=", i.ToString) 08 Try 09 Console.WriteLine(score(i)) 10 Catch e As IndexOutOfRangeException 11 Console.WriteLine() 12 Console.WriteLine("例外處理類型 :{0}", e.GetType.ToString) 13 Console.WriteLine("錯誤訊息 :{0}", e.Message) 14 Console.WriteLine("程式或物件名稱 :{0}", e.Source) 15 Console.WriteLine("產生錯誤程序 :{0}", e.TargetSite.Name) 16 Console.WriteLine("錯誤之處 :{0}", e.StackTrace) 17 Finally 18 Console.WriteLine("index = {0}", i.ToString) 19 End Try 20 Next 21 Console.Read() 22 End Sub 24 End Module

  10. 【程式碼】 FileName:TryCatch-4.sln 01 Module Module1 03 Sub Main() 04 Dim i, n, f As Short 05 Do While (True) 06 Try 07 Console.Write("n = ") 08 n = Console.ReadLine() 09 f = 1 10 For i = 1 To n 11 f = f * i 12 Next 13 Exit Do 14 Catch e1 As InvalidCastException 15 Console.WriteLine("資料型態錯誤") 16 Catch e2 As OverflowException 17 Console.WriteLine("溢位錯誤") 18 Catch e3 As Exception 19 Console.WriteLine("其他錯誤") 20 End Try 21 Loop 22 Console.WriteLine("{0} != {1}", n, f) 23 Console.ReadLine() 24 End Sub 26 End Module

  11. 9.3 自訂例外處理-使用Throw敘述 遇到特殊錯誤,系統並沒有提供此種判斷,自己也可以寫程式來處理特殊錯誤,Throw敘述即可指定要用到那個例外類別。參考下面範例。

  12. 【程式碼】 FileName:ThrowDemo.sln 01 Module Module1 03 Sub Main() 04 Dim month As Integer 05 Do While (True) 06 Try 07 KeyinMonth(month) 08 Exit Do 09 Catch e As ArgumentOutOfRangeException 10 Console.WriteLine("不合理月份") 11 Catch e As Exception 12 Console.WriteLine("其他種錯誤") 13 End Try 14 Loop 15 Console.ReadLine() 16 End Sub 18 Public Sub KeyinMonth(ByRef month As Integer) 19 Console.Write("輸入月份 ( 1 - 12 ) : ") 20 month = Console.ReadLine() 21 If (month < 1 Or month > 12) Then 22 Throw New ArgumentOutOfRangeException() 23 End If 24 End Sub 25 End Module

  13. 9.4 例外類別繼承 • 若 .NET Framework所提供的例外類別不夠使用的話,我們可以使用物件導向繼承的機制,將系統提供的例外類別加入延伸、增加功能,而且您也可以覆寫原有系統例外類別的方法或屬性,或是增加新的方法或屬性。請看下面範例示範。

  14. 【程式碼】 FileName:InheritsException.sln 01 Class SalaryException ‘定義SalaryException例外繼承自Exception例外類別 02 Inherits Exception 03 04 Public Overrides Function ToString() As String ‘覆寫ToString()方法 05 Return "發生Salary例外類別" 06 End Function 07 ‘覆寫Message屬性, 該屬性是唯讀屬性 08 Public Overrides ReadOnly Property Message() As String 09 Get 10 Return "薪水不能設定負數或零" 11 End Get 12 End Property 13 14 Public Sub ShowMsg() ‘新增ShowMsg方法 15 Console.WriteLine("沒良心的老闆,設定員工薪水請小心...^_|||") 16 End Sub 17 End Class

  15. 19 Class Empolyee ‘定義Empolyee員工類別 20 Private _name As String ‘私有_name欄位 21 Private _salary As Integer ‘私有_salary欄艱 22 Sub New(ByVal name As String) ‘Empolyee員工類別的建構式,用來設定員工姓名 23 _name = name 24 End Sub 25 Property Salary() As Integer ‘薪水屬性 26 Get 27 Return _salary 28 End Get 29 Set(ByVal value As Integer) ‘ 如果薪水小於等於0,則產生SalaryException例外類別物件 30 If value <= 0 Then 31 Console.WriteLine("員工 {0} 設定薪水 {1} 失敗", _name, value) 32 Throw New SalaryException ‘ 產生SalaryException例外類別物件 33 Else 34 _salary = value 35 End If 36 End Set 37 End Property

  16. 39 Sub ShowSalary() 40 Console.WriteLine("員工 {0} 的薪水 {1}", _name, Salary) 41 End Sub 42 End Class 44 Module Module1 46 Sub Main() 47 Try 48 Dim tom As New Empolyee("湯姆") 49 tom.Salary = 50000 50 tom.ShowSalary() 51 Console.WriteLine("====================================") 52 Dim peter As New Empolyee("彼得") ‘設定Peter物件的薪水為負數,因此會產生SalaryException例外類別 53 peter.Salary = -10000 54 peter.ShowSalary() 55 Catch ex As SalaryException ‘補捉SalaryException例外 56 Console.WriteLine(ex.ToString)’呼叫SalaryException的ToString方法 57 Console.WriteLine(ex.Message)’呼叫SalaryException的Message屬性 58 ex.ShowMsg() ‘呼叫SalaryException的ShowMsg方法 59 End Try 60 Console.Read() 61 End Sub 63 End Module

More Related