Exception Handling API
Exception handling in the framework is implemented based on my article Exception Handling in Real-Life Java Applications published on DZone.com.
Main Concepts
The main concepts about exception handling:
-
If you have checked exceptions, always have only one catch statement.
-
Never catch Runtime exception
-
In the catch statement just call
JK.throww(e);
the framework will handle the rest.-
In web applications, the exception will be logged and then redirected to the error page.
-
In AJAX requests, the exception will be logged and then shown as an error message to the users.
-
The exception will be redirected to the properly registered exception handler.
-
-
Use
JK.exception("message")
to throw an exception. -
If you are handling an exception inside a method that should return value, it is safe to return null after
JK.throw(e)
.
Example
package com.app.exception;
import com.jk.core.util.JK;
public class ExceptionExample1 {
public static void main(String[] args) {
//Throw null pointer exception and handle with default handler, which log the error then through runtime exception
try {
String name = null;
name.length();
} catch (Exception e) {
JK.handle(e);
}
}
}
Custom Exceptions Handling
You can register custom exception handlers to scan and handle your exception.
To create a custom exception handler:
-
Create a Custom Handler that is annotated with
@ExceptionHandler
and implementsJKExceptionHandler<YourException>
. -
Inform the API about your exception handlers by calling:
JKExceptionHandlerFactory.getInstance().registerHandlers(PACKAGE_NAMES);
This will register all the handlers, all the handlers, in the provided packages.
Example
package com.app;
import javax.swing.JOptionPane;
import com.jk.util.exceptions.handler.ExceptionHandler;
import com.jk.util.exceptions.handler.JKExceptionHandler;
@ExceptionHandler
public class ArrayIndexExceptionHandler implements JKExceptionHandler<ArrayIndexOutOfBoundsException> {
@Override
public void handle(ArrayIndexOutOfBoundsException throwable, boolean throwRuntimeException) {
JOptionPane.showMessageDialog(null, "My ArrayIndexException Handler");
}
}
-
Register the Handler and call the handler:
package com.app;
import com.jk.util.JK;
import com.jk.util.exceptions.handler.JKExceptionHandlerFactory;
public class ExceptionExample2 {
public static void main(String[] args) {
JKExceptionHandlerFactory.getInstance().registerHandlers("com.app");
//Throw nullpointer exception and handle with custom handler, which show the message in JOptionPane
try {
int arr[]=new int[0];
int x=arr[1];
} catch (Exception e) {
JK.handle(e);
}
}
}
Registering Default Handler
Sometimes, it’s useful to register a default handler for any unregistered exception. DefaultHandler can be set using:
JKExceptionHandlerFactory.getInstance().setDefaultExceptionHandler(YOUR_DEFAULT_EXCEPTION_HANDLER);
Exception Handling in Web Environment
In web, webstack, or microservices applications, by default, the framework will
look for the com.app
package and register any handler in it or its sub-packages.
If you need to override the default, you can set the jk.core.exception.handlers
property in your
configuration for any comma-separated-value of packages, for example:
jk.core.exception.handlers=com.jalalkiswani.demo, com.jalalkiswani.lib
JKWebContextListener
will handle the handler’s registration.
Framework Default Behavior
The default exception handler in the framework behaves as follows:
Standalone Apps
In the non-web environment, the default handler will log the exception using a logger.error
, then through the error.
Web Apps
In Web apps, the default will log the exception using a logger.error
then return to JKFacesExceptionHandler
to
forward to the right error page or show an Ajax error message.
WebStack Apps
In WebStack apps, the handler will insert an event in the cmon_event
table in the database, if the
entity com.jk.webstack.services.mointor.Event
is available in db-entities-packages
property, then return to JKFacesExceptionHandler
to
forward to the right error page or show an Ajax error message.