1: Private Sub checkLicense()
2: If Dir("c:\user\license.txt") = "" Then
3: Err.Raise 1234
4: End If
5: End Sub
6:
7: Private Sub Form_Load()
8: On Error GoTo licenseErrorHandler
9: checkLicense
10: Exit Sub
11:
12: licenseErrorHandler:
13: If Err.Number <> 1234 Then
14: Err.Raise Err.Number
15: Else
16: MsgBox "ライセンスファイルがありません。使用するためにはライセンス認証が必要です。"
17: End
18: End If
19: End Sub
1: Private Sub checkLicense()
2: If Dir("c:\user\license.txt") = "" Then
3: Err.Raise(1234)
4: End If
5: End Sub
6:
7: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
8: On Error GoTo licenseErrorHandler
9: checkLicense()
10: Exit Sub
11:
12: licenseErrorHandler:
13: If Err.Number <> 1234 Then
14: On Error GoTo 0
15: Else
16: MsgBox("ライセンスファイルがありません。使用するためにはライセンス認証が必要です。")
17: End
18: End If
19: End Sub
1: Class LicenseErrorException
2: Inherits Exception
3: Public Sub New(ByVal msg As String)
4: MyBase.New(msg)
5: End Sub
6: End Class
7:
8: Private Sub checkLicense()
9: If Dir("c:\user\license.txt") = "" Then
10: Throw New LicenseErrorException("ライセンスファイルがありません。")
11: End If
12: End Sub
13:
14: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
15: Try
16: checkLicense()
17: Catch ex As LicenseErrorException
18: MsgBox(ex.Message & "使用するためにはライセンス認証が必要です。")
19: End
20: End Try
21: End Sub
1: Private Function GetData(ByVal filename As String)
2: On Error GoTo errorHandlerInCalc
3: Open "c:\" & filename & ".txt" For Input As #1
4: Close 1
5: Exit Function
6:
7: errorHandlerInCalc:
8: If Err.Number <> 53 Then
9: Err.Raise Err.Number
10: Else
11: Err.Raise 1234
12: End If
13: End Function
14:
15: Private Sub Form_Load()
16: On Error GoTo errorHandlerInMain
17: Debug.Print GetData("app_define_data")
18: Exit Sub
19:
20: errorHandlerInMain:
21: If Err.Number <> 1234 Then
22: Err.Raise Err.Number
23: Else
24: MsgBox "内部パラメータエラーです。"
25: End If
26: End Sub
リスト9-55 エラー処理の中で、さらにエラーを発生させているプログラム
これを実行すると図9-56のようになる。
●図9-56 リスト9-55の実行結果
このソースは、ほとんどそのままVB.NETでも動作する(リスト9-57)。
1: Private Function GetData(ByVal filename As String)
2: On Error GoTo errorHandlerInCalc
3: FileOpen(1, "c:\" & filename & ".txt", OpenMode.Input)
4: FileClose(1)
5: Exit Function
6:
7: errorHandlerInCalc:
8: If Err.Number <> 53 Then
9: Err.Raise(Err.Number)
10: Else
11: Err.Raise(1234)
12: End If
13: End Function
14:
15: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
16: On Error GoTo errorHandlerInMain
17: Trace.WriteLine(GetData("app_define_data"))
18: Exit Sub
19:
20: errorHandlerInMain:
21: If Err.Number <> 1234 Then
22: Err.Raise(Err.Number)
23: Else
24: MsgBox("内部パラメータエラーです。")
25: End If
26: End Sub
リスト9-57 リスト9-55をVB.NETで書き換えたプログラム
これを実行すると以下のようになる。
●図9-58 リスト9-57の実行結果
これを、構造化例外処理を用いて書き直すと以下のようになる(リスト9-59)。
1: Class InternalParameterErrorException
2: Inherits Exception
3: Public Sub New(ByVal msg As String, ByVal innerException As Exception)
4: MyBase.New(msg, innerException)
5: End Sub
6: End Class
7:
8: Private Function GetData(ByVal filename As String)
9: Try
10: FileOpen(1, "c:\" & filename & ".txt", OpenMode.Input)
11: FileClose(1)
12: Return "ファイルから読み出したつもりのダミーデータ"
13: Catch ex As System.IO.FileNotFoundException
14: Throw New InternalParameterErrorException("内部パラメータエラーです。", ex)
15: End Try
16: End Function
17:
18: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
19: Try
20: Trace.WriteLine(GetData("app_define_data"))
21: Catch ex As InternalParameterErrorException
22: MsgBox(ex.Message & " (" & ex.InnerException.Message & ")")
23: End Try
24: End Sub