{"id":49,"date":"2009-11-03T23:16:54","date_gmt":"2009-11-03T22:16:54","guid":{"rendered":"http:\/\/www.gennard.net\/blog\/?p=49"},"modified":"2009-11-03T23:16:54","modified_gmt":"2009-11-03T22:16:54","slug":"object-coboljava-default-exception-handler","status":"publish","type":"post","link":"http:\/\/www.gennard.net\/blog\/2009\/11\/object-coboljava-default-exception-handler\/","title":{"rendered":"Object COBOL\/Java default exception handler"},"content":{"rendered":"<p>While working on some support recently for our compiler (Micro Focus COBOL compiler that is), I became annoyed with the lack of a reasonable error messages\/stack trace output from our Java\/COBOL Object support.<\/p>\n<p>I have no idea why our default exception handler for Java exceptions just displays a such a simple message with little or no information.   <\/p>\n<p>For those who have not seen it, display something similar to:<\/p>\n<p><code lang=\"text\" width=\"700\" lines=\"-1\" nowrap=\"0\" >Exception 65537 not trapped by the class javaexceptionmanager.<br \/>\nDescription: \"Java exception\"<br \/>\nTest error<br \/>\nHit T to terminate program. Hit any other key to continue.<br \/>\ninstantiated - test <\/p>\n<p>Exception 65537 not trapped by the class javaexceptionmanager.<br \/>\nDescription: \"Java exception\"<br \/>\nTest error<br \/>\nHit T to terminate program. Hit any other key to continue.<br \/>\n<\/code><\/p>\n<p>Luckily for me and you, we do expose a mechanism for replacing the default exception handler.<\/p>\n<p>Anyway, with very little effort I created a different &#8220;default&#8221; system exception handler that display this instead:<\/p>\n<p><code lang=\"text\">instantiated - test 1<br \/>\njava.lang.Exception: Test error<br \/>\n        at SimpleClass.TestException(SimpleClass.java:10)<\/p>\n<p>WARNING: JavaException: Test error<br \/>\ninstantiated - test 2<br \/>\njava.lang.Exception: Test error<br \/>\n        at SimpleClass.TestException(SimpleClass.java:10)<br \/>\nWARNING: JavaException: Test error<br \/>\n<\/code><\/p>\n<p>The key difference being that a Java stack trace is included, boy did this help me.<\/p>\n<p>The program below is the code that implements the exception handler.  I am sure someone else can take this example and make it much nicer and provide more features but for this blog I will keep it simple.<\/p>\n<p><code lang=\"cobol\" width=\"700\" lines=\"-1\" nowrap=\"0\"><br \/>\n      $set ooctrl (+p-f) case<br \/>\n       program-id. ExceptionCatcher.<\/p>\n<p>       class-control.<br \/>\n             EntryCallback is class          \"entrycll\"<br \/>\n             JavaExceptionManager is class   \"javaexpt\"<br \/>\n             ExceptionManager is class       \"exptnmgr\"<br \/>\n             Javasup is class \"javasup\"<br \/>\n             .<\/p>\n<p>       working-storage section.<br \/>\n       01 wsCallback                   object reference.<br \/>\n       01 wsIterator                   object reference.<br \/>\n       01 theJavaException             object reference.<br \/>\n       local-storage section.<br \/>\n       01 filler pic x.   *> dummy storage item to allow recursion<br \/>\n       linkage section.<br \/>\n       01 lnkException                 object reference.<br \/>\n       01 lnkErrorObject               object reference.<br \/>\n       01 lnkErrorTextCollection       object reference.<br \/>\n       01 lnkErrorNumber               pic x(4) comp-5.<br \/>\n       01 anElement                    object reference.<br \/>\n         procedure division.<br \/>\n      *>---Set up system level Exception handler<br \/>\n             invoke EntryCallback \"new\" using<br \/>\n                    z\"JException\"<br \/>\n                    returning wsCallback<br \/>\n             end-invoke<\/p>\n<p>             invoke ExceptionManager \"register\" using<br \/>\n                    javaexceptionmanager<br \/>\n                    wsCallback<br \/>\n             end-invoke<\/p>\n<p>             invoke EntryCallback \"new\" using z\"DispError\"<br \/>\n               returning wsIterator<br \/>\n             end-invoke<\/p>\n<p>             goback.<\/p>\n<p>         entry \"Jexception\" using<br \/>\n                  lnkException<br \/>\n                  lnkErrorNumber<br \/>\n                  lnkErrorTextcollection<br \/>\n             .<\/p>\n<p>              invoke javasup \"exceptionOccurred\"<br \/>\n                   returning theJavaException<br \/>\n              end-invoke<\/p>\n<p>              if theJavaException not equal null<br \/>\n                invoke theJavaException \"printStackTrace\"<br \/>\n              end-if<\/p>\n<p>              invoke lnkErrorTextCollection \"do\" using wsIterator<br \/>\n              goback.<br \/>\n             .<\/p>\n<p>          entry \"DispError\" using anElement<br \/>\n            display \"WARNING: JavaException: \" with no advancing<br \/>\n            invoke anElement \"display\"<br \/>\n            display \" \"<br \/>\n            goback.<br \/>\n             .<br \/>\n<\/code><\/p>\n<p>To use the above code, you just have to first cut-paste to code into a file called ExceptionCatcher.cbl and include this in your project, and then add the directive INITCALL&#8221;ExceptionCatcher&#8221;, then away you go.<\/p>\n<p>My test programs for the above example are:<\/p>\n<p><code lang=\"cobol\" width=\"700\" lines=\"-1\" nowrap=\"0\" ><br \/>\n     $set ooctrl (+p-f) case<br \/>\n       program-id. jtest.<\/p>\n<p>       class-control.<br \/>\n             SimpleClass is class            \"$JAVA$SimpleClass\"<br \/>\n             .<\/p>\n<p>       working-storage section.<br \/>\n       01 theInstance                  object reference.<br \/>\n       local-storage section.<br \/>\n       01 filler pic x.   *> dummy storage to allow the local entry<br \/>\n         procedure division.<br \/>\n      *>---Instantiate the class<br \/>\n             invoke SimpleClass \"new\" returning theInstance<\/p>\n<p>             display \"instantiated - test 1\"<br \/>\n             invoke theInstance \"TestException\"<\/p>\n<p>             display \"instantiated - test 2\"<br \/>\n             invoke theInstance \"TestException\"<br \/>\n             stop run.<\/p>\n<p><\/code><\/p>\n<p>and the Java class itself:<\/p>\n<p><code lang=\"Java\"><br \/>\nimport java.lang.* ;<\/p>\n<p>public class SimpleClass {<br \/>\n  public SimpleClass() { }<\/p>\n<p>  public void TestException() throws Exception {<br \/>\n     throw new Exception (\"Test error\" );<br \/>\n  }<br \/>\n}<br \/>\n<\/code><\/p>\n<p>References: <a href=\"http:\/\/supportline.microfocus.com\/documentation\/books\/nx60\/nx60indx.htm\">Studio Enterprise 6.0 Document for COBOL\/Java Interop<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working on some support recently for our compiler (Micro Focus COBOL compiler that is), I became annoyed with the lack of a reasonable error messages\/stack trace output from our Java\/COBOL Object support. I have no idea why our default &hellip; <a href=\"http:\/\/www.gennard.net\/blog\/2009\/11\/object-coboljava-default-exception-handler\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,48,32],"tags":[209,111,239],"_links":{"self":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/49"}],"collection":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":0,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}