「VB.NET」throw文で例外を投げるサンプル

2021年9月14日

構文
Throw [ expression ]
スローされる例外に関する情報を提供します
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Module Module1
Sub Main()
Dim cftA As Integer = 99
'try catch例外の処理
Try
Dim result As Integer
'関数funcAを呼び出す
result = funcA(cftA)
Catch ex As Exception
'例外メッセージを出力
Console.WriteLine(ex.Message)
Finally
'Finally処理
Console.WriteLine("例外処理が終了しました")
End Try
Console.ReadKey()
End Sub
'関数funcAの定義
Private Function funcA(ByVal cftA As Integer) As Integer
If cftA = 99 Then
Throw New Exception("変数の値" & cftA & "が例外エラーです")
End If
Return cftA * 2
End Function
End Module
Module Module1 Sub Main() Dim cftA As Integer = 99 'try catch例外の処理 Try Dim result As Integer '関数funcAを呼び出す result = funcA(cftA) Catch ex As Exception '例外メッセージを出力 Console.WriteLine(ex.Message) Finally 'Finally処理 Console.WriteLine("例外処理が終了しました") End Try Console.ReadKey() End Sub '関数funcAの定義 Private Function funcA(ByVal cftA As Integer) As Integer If cftA = 99 Then Throw New Exception("変数の値" & cftA & "が例外エラーです") End If Return cftA * 2 End Function End Module
Module Module1

    Sub Main()
        Dim cftA As Integer = 99

        'try catch例外の処理
        Try
            Dim result As Integer

            '関数funcAを呼び出す
            result = funcA(cftA)
        Catch ex As Exception

            '例外メッセージを出力
            Console.WriteLine(ex.Message)

        Finally
            'Finally処理
            Console.WriteLine("例外処理が終了しました")
        End Try

        Console.ReadKey()

    End Sub

    '関数funcAの定義
    Private Function funcA(ByVal cftA As Integer) As Integer

        If cftA = 99 Then
            Throw New Exception("変数の値" & cftA & "が例外エラーです")
        End If

        Return cftA * 2
    End Function


End Module

結果
変数の値99が例外エラーです
例外処理が終了しました

VB.net

Posted by arkgame