[VB.NET]try catchで例外処理を行うサンプル
書式
Try
//処理コード
Catch ex As ArithmeticException
使用例
Module Module1
Sub Main()
Dim cftA As Integer = 25
Dim cftB As Integer = 0
'try catch例外の処理
Try
Dim result As Integer
'関数funcAを呼び出す
result = funcA(cftA, cftB)
Catch ex As ArithmeticException
'例外メッセージを出力
Console.WriteLine(ex.Message)
Finally
'Finally処理
Console.WriteLine("例外処理が終了しました")
End Try
Console.ReadKey()
End Sub
'関数funcAの定義
Private Function funcA(ByVal cftA As Integer, ByVal cftB As Integer) As Integer
Return cftA / cftB
End Function
End Module
Module Module1
Sub Main()
Dim cftA As Integer = 25
Dim cftB As Integer = 0
'try catch例外の処理
Try
Dim result As Integer
'関数funcAを呼び出す
result = funcA(cftA, cftB)
Catch ex As ArithmeticException
'例外メッセージを出力
Console.WriteLine(ex.Message)
Finally
'Finally処理
Console.WriteLine("例外処理が終了しました")
End Try
Console.ReadKey()
End Sub
'関数funcAの定義
Private Function funcA(ByVal cftA As Integer, ByVal cftB As Integer) As Integer
Return cftA / cftB
End Function
End Module
Module Module1 Sub Main() Dim cftA As Integer = 25 Dim cftB As Integer = 0 'try catch例外の処理 Try Dim result As Integer '関数funcAを呼び出す result = funcA(cftA, cftB) Catch ex As ArithmeticException '例外メッセージを出力 Console.WriteLine(ex.Message) Finally 'Finally処理 Console.WriteLine("例外処理が終了しました") End Try Console.ReadKey() End Sub '関数funcAの定義 Private Function funcA(ByVal cftA As Integer, ByVal cftB As Integer) As Integer Return cftA / cftB End Function End Module
結果
算術演算の結果オーバーフローが発生しました。
例外処理が終了しました