Java Exception

In java, exception structure  is like this :

                       Throwable
                          |
                          |
                      Exception
                  /       |         \
                /         |           \
RunTimeException  SpecificException1   SpecificException2
       |
       |
SpecificException3

Each class extends another one and the exception class extends throwable.

When you need to throw a custom exception, you need to create a customException which extends another Exception like RunTimeException, Exception or anotherCustomException.

Exemple:
public class ConflictException extends RuntimeException {

    public ConflictException() {
    }

    public ConflictException(String message) {
        super(message);
    }

    public ConflictException(String message, Throwable cause) {
        super(message, cause);
    }

    public ConflictException(Throwable cause) {
        super(cause);
    }

    public ConflictException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

 

The rule is to catch only exception when you want to add some information. Then you can throw it back like this :

catch (WhateverException e) {
    throw e;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s