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.
- 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, .
- Change the
Superclass to
java.lang.Exception.
- Click
Finish to create the file.
- Overwrite the skeleton contents of the file with the contents of the
JavaBookException.java file that you downloaded previously.
- Click
File > Save.
Comments
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.