!--a11y-->
General
Rules and Guidelines for Managing Exceptions 
Exception handling influences the quality of Java programs because:
· It ensures that the program can continue in error conditions.
· It allows the user to find the problem, which caused a thread to terminate. An exception that caused the termination of a portal application should lead to the causing problem by giving meaningful information.
Coding Rules
Rule |
Error Example |
Explanation |
Always pass the original exception |
catch (IOException e) { throw new RuntimeException("Problem in I/O."); }
|
The original problem may be hidden if you do not do this. The original exception can be passed on by adding it to the message: ("message: " + e); It is better to use a constructor that can take an exception as argument as well, for example: new PortalRuntimeException("Error in I/O.", e) |
Always include a message |
throw new IOException();
|
There is always additional information which is useful to the user. If possible this information should be constructed dynamically. However, this should not introduce problems. The following example produces a NullPointerException if the context is null: throw new RuntimeException("Error in "+ context.getComponent()); |
Do not make wrong assumptions |
catch (NamingException e) { throw new PortalRuntimeException("Registry not found:" + e); }
|
There are many subclasses of NamingException and the reason may be other |
Do not have empty exception handlers |
catch (Exception e) { // ignore }
|
|
Do not only print the stack trace |
catch (NamingException e) { e.printStackTrace(); } |
If stdout is not redirected, the exception is only visible on the console. The console, however, may not always be there or accessible, for example the application was started using javaw which does not open a console. In addition, there is sometimes too much output on the console thus making it impossible to follow the output. The exception should be logged. |
Log important Exception |
catch
(IOException e) { |
In general, when important information is lost in an exception handler because it is not passed on, it should be logged. The following line logs the stack trace of the exception above: PortalRuntime.getLogger().severe(e, " Exception while reading service parameters."); |
The portal platform enables a developer to add extra information to exceptions thrown by portal applications, by proposing an Exception catalog and a tool for displaying this information to the users. The objective of this approach is to help you diagnose quickly and effectively any problems that occur in the portal platform.
The exception catalog is located in the private errors folder of the LogViewer portal application. It contains a predefined list of exception files, either sent by the portal or representing the standard Java exception.
