Java からスローされた例外は、javaexpt クラスに発生したネイティブのオブジェクト指向 COBOL の例外として、COBOL に返されます。デフォルトでは、COBOL ランタイム システムは例外を受信すると、その旨を警告するメッセージを表示して終了します。あるいは、COBOL プログラムに例外ハンドラーを追加して、例外をトラップすることも可能です。
Java 例外をトラップする手順は次のとおりです。
repository. ... class JavaExceptionManager as "javaexpt" class ExceptionManager as "exptnmgr" class Callback as "callback" class EntryCallback as "entrycll" ...
invoke Callback "new" using anObject z"methodName" returning aHandler
EntryCallback の場合、次のように記述します。
invoke EntryCallback "new" using z"entryPointname" returning aHandler
invoke ExceptionManager "register" using JavaExceptionManager aHandler
以上の手順を実施すると、ネイティブのオブジェクト指向 COBOL の Java ドメインを経由して呼び出している、Java クラスからスローされた Java 例外が、作成した例外ハンドラーに常に送信されるようになります。
Java プログラムからスローされた例外をキャッチする COBOL プログラムの例を次に示します。
$set ooctrl (+p-f) program-id. ExceptionCatcher. class-control. SimpleClass is class "$JAVA$SimpleClass" EntryCallback is class "entrycll" JavaExceptionManager is class "javaexpt" ExceptionManager is class "exptnmgr" . working-storage section. 01 theInstance object reference. 01 wsCallback object reference. local-storage section. 01 filler pic x. *> dummy storage to allow the local entry *> point to be used for the callback linkage section. 01 lnkException object reference. procedure division. *>---Set up Exception handler invoke EntryCallback "new" using z"JException" returning wsCallback invoke ExceptionManager "register" using javaexceptionmanager wsCallback *>---Instantiate the class invoke SimpleClass "new" returning theInstance display "instantiated" invoke theInstance "TestException" display "excepted" stop run. entry "Jexception" using lnkException. invoke lnkException "display" .
このプログラムで局所記憶節によって再帰呼び出しが可能になっており、COBOL ランタイム システムが EntryCallback を再帰的に呼び出します。局所記憶節がないと、ランタイム システム エラーの原因となります。呼び出される SimpleClass の Java コードは次のとおりです。
import java.lang.* ; public class SimpleClass { public SimpleClass() { } public void TestException() throws Exception { Exception e = new Exception ("Test error" ); throw e; } }
COBOL 開発システム インストールの help\mfcobol.docs.zip にある「Java Run-time Class Library Reference」を参照してください。