Converting Status Codes to Exceptions

Error handling in Java is different from procedural languages such as COBOL. Java uses exceptions, as opposed to error codes or status codes, to deal with failure and unexpected conditions. As the COBOL book tutorial program returns a status code, you need to convert this status code into a Java exception to make it suitable for the JVM.
  1. Create a new Java class using the same package as the bean, but call it JavaBookException.

    If you cannot remember how to create a new class, refer to the previous topic, Creating the Java Bean.

  2. Change the Superclass to java.lang.Exception.
  3. Click Finish to create the file.
  4. Overwrite the skeleton contents of the file with the contents of the file that you downloaded previously (the file is stored in C:\myfiles\COBOL_JSP_DEMO_2_2_SRC\JSPBookDemo\src\com\microfocus\book).
  5. Click File > Save.


In this Java code sample, note that there is a constructor that takes a string:
public JavaBookException(String statusCode)
The following string maps the file status field in the COBOL program:
01 ls-file-status   pic xx.
Using this statusCode construct, the Java program translates known COBOL status codes to appropriate error messages via its Map<String, String> construct, with a static string for any unknown error. Finally, the error exception is passed to the superclass constructor, and the status code is stored internally.
Note: The element serialVersionUId is used by the Java serialization mechanism to distinguish between different versions of a class, and it has no impact on the program.