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 exception, 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 then redirected to error page.

    • In AJAX requests, the exception will be logged then shown as error message to to the users.

    • The exception will be redirected to the proper registered exception handler.

  • Use JK.exception("message") to throw an exception.

  • If you are handling an exception inside method that should return value, it is safe to return null after JK.throw(e).


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;
		} catch (Exception e) {

Custom Exceptions Handling

You can register custom exception handlers to scan handle your exception.

To create customer exception handler:

  1. Create Custom Handler that is annotated with @ExceptionHandler and implements JKExceptionHandler<YourException>.

  2. Inform the API about your exception handlers by calling:


This will register all the handlers all the handlers, in th provided packages.


package com.app;

import javax.swing.JOptionPane;

import com.jk.util.exceptions.handler.ExceptionHandler;
import com.jk.util.exceptions.handler.JKExceptionHandler;

public class ArrayIndexExceptionHandler implements JKExceptionHandler<ArrayIndexOutOfBoundsException> {

    public void handle(ArrayIndexOutOfBoundsException throwable, boolean throwRuntimeException) {
    JOptionPane.showMessageDialog(null, "My ArrayIndexException Handler");

  1. Register 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) {
	//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) {

Registering Default Handler

Sometimes, its useful to register default handler for any unregistered exception. DefaultHandler can be set using:


Exception Handling in Web Environment

In web, webstack, or microservices applications, by default, the framework will look for 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 handler the handlers registration.

Framework Default Behavior

The default exception handler in the framework behave as follows:

Standalone Apps

In non web environment, the default handler, will log the exception using logger.error, then through the error.

Web Apps

In Web apps, the default will log the exception using logger.error then return to JKFacesExceptionHandler to forward to the right error page or show Ajax error message.

WebStack Apps

In WebStack apps, the handler will insert an event in 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 Ajax error message.

Microservice apps

In Microservices apps, JKRestExceptionHandler send the exception the Unified Logging Microservice if enabled, then return proper error to the client.