「VB.NET」例外処理Finallyのサンプル
構文
Try
処理コード
Catch ex As 例外の種類
例外が発生した場合の処理
Finally
例外発生有無に関わらず実行される処理
End Try
Try
処理コード
Catch ex As 例外の種類
例外が発生した場合の処理
Finally
例外発生有無に関わらず実行される処理
End Try
Try 処理コード Catch ex As 例外の種類 例外が発生した場合の処理 Finally 例外発生有無に関わらず実行される処理 End Try
使用例
Module Module1
Sub Main()
Dim aa As Integer
aa = testfun(12, 0)
Console.WriteLine(aa)
Console.ReadKey()
End Sub
Public Function testfun(ByVal x As Decimal, ByVal y As Decimal) As Integer
Try
x /= y
' 例外
Catch ex As DivideByZeroException
Console.WriteLine(ex.Message)
Finally
Console.WriteLine("例外終了メッセージ")
End Try
testfun = 10
End Function
End Module
Module Module1
Sub Main()
Dim aa As Integer
aa = testfun(12, 0)
Console.WriteLine(aa)
Console.ReadKey()
End Sub
Public Function testfun(ByVal x As Decimal, ByVal y As Decimal) As Integer
Try
x /= y
' 例外
Catch ex As DivideByZeroException
Console.WriteLine(ex.Message)
Finally
Console.WriteLine("例外終了メッセージ")
End Try
testfun = 10
End Function
End Module
Module Module1 Sub Main() Dim aa As Integer aa = testfun(12, 0) Console.WriteLine(aa) Console.ReadKey() End Sub Public Function testfun(ByVal x As Decimal, ByVal y As Decimal) As Integer Try x /= y ' 例外 Catch ex As DivideByZeroException Console.WriteLine(ex.Message) Finally Console.WriteLine("例外終了メッセージ") End Try testfun = 10 End Function End Module
実行結果
0 で除算しようとしました。
例外終了メッセージ
10