例外処理

このヘルプでは、COBOL、C#、VB.NET の間にある例外処理方法の相違点について説明します。

COBOL の例外処理

*> Throw an exception
01 up type Exception value new Exception(“Something is really wrong.");
raise up

*> Catch an exception. ex is a predefined data item of type System.Exception.
try
  set y to 0;
  compute x = 10 / y
catch ex                 *> Argument is optional, no "When" keyword
  display ex::Message
finally
  invoke type Microsoft.VisualBasic.Interaction::Beep
end-try

C# の例外処理

// Throw an exception
Exception up = new Exception("Something is really wrong.");
throw up;  // ha ha

// Catch an exception
try
{
  y = 0;
  x = 10 / y;
}
catch (Exception ex) // Argument is optional, no "When" keyword
{
  Console.WriteLine(ex.Message);
}
finally
{
  Microsoft.VisualBasic.Interaction.Beep();
}

VB.NET の例外処理

' Throw an exception
Dim ex As New Exception("Something is really wrong.")
Throw  ex

' Catch an exception
Try
  y = 0
  x = 10 / y
Catch ex As Exception When y = 0 ' Argument and When is optional
  Console.WriteLine(ex.Message)
Finally
  Beep()
End Try

' Deprecated unstructured error handling
On Error GoTo MyErrorHandler
...
MyErrorHandler: Console.WriteLine(Err.Description)

これらの例の一部は、ハーディング大学コンピューター サイエンス学部の Frank McCown 博士が作成したもので、クリエイティブ コモンズ ライセンスに基づいて使用が許可されています。