Wednesday 19 August 2015

Exception hierarchy

java.lang.Exception
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions.
                                    
Exception hierarchy

The class Exception and its subclasses are a form of Throwable.

All the subclass of Exception class (except Sub class of RuntimeException) are called  checked exceptions.

Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
Complete Hierarchy

Exception extends Throwable
public class Exception extends Throwable {
    static final long serialVersionUID = -3387516993124229948L;

    public Exception() {
        super();
    }

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

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

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

    protected Exception(String message, Throwable causeboolean enableSuppression,
                        boolean writableStackTrace) {
        super(messagecauseenableSuppressionwritableStackTrace);
    }
}

Unchecked Exception extends RuntimeException
package java.lang;
public class NullPointerException extends RuntimeException {
    private static final long serialVersionUID = 5162710183389028792L;


    public NullPointerException() {
        super();
    }


    public NullPointerException(String s) {
        super(s);
    }
}

Checked Exception extends Exception

public class SQLException extends Exception implements Iterable<Throwable> {

    public SQLException(String reason, String SQLStateint vendorCode) {
        super(reason);
        this.SQLState = SQLState;
        this.vendorCode = vendorCode;
        if (!(this instanceof SQLWarning)) {
            if (DriverManager.getLogWriter() != null) {
                DriverManager.println("SQLState("+SQLState+
                                 ") vendor code(" + vendorCode + ")");
                printStackTrace(DriverManager.getLogWriter());
            }
        }
    }

    public SQLException(String reason, String SQLState) {
        super(reason);
        this.SQLState = SQLState;
        this.vendorCode = 0;
        if (!(this instanceof SQLWarning)) {
            if (DriverManager.getLogWriter() != null) {
                printStackTrace(DriverManager.getLogWriter());
                DriverManager.println("SQLException:SQLState("+SQLState+")");
            }
        }
    }

    public SQLException(String reason) {
        super(reason);
        this.SQLState = null;
        this.vendorCode = 0;
        if (!(this instanceof SQLWarning)) {
            if (DriverManager.getLogWriter() != null) {
                printStackTrace(DriverManager.getLogWriter());
            }
        }
    }

    public SQLException() {
        super();
        this.SQLState = null;
        this.vendorCode = 0;
        if (!(this instanceof SQLWarning)) {
            if (DriverManager.getLogWriter() != null) {
                printStackTrace(DriverManager.getLogWriter());
            }
        }
    }

    public SQLException(Throwable cause) {
        super(cause);

        if (!(this instanceof SQLWarning)) {
            if (DriverManager.getLogWriter() != null) {
                printStackTrace(DriverManager.getLogWriter());
            }
        }
    }

    public SQLException(String reason, Throwable cause) {
        super(reason,cause);

        if (!(this instanceof SQLWarning)) {
            if (DriverManager.getLogWriter() != null) {
                    printStackTrace(DriverManager.getLogWriter());
            }
        }
    }

    public SQLException(String reason, String sqlState, Throwable cause) {
        super(reason,cause);

        this.SQLState = sqlState;
        this.vendorCode = 0;
        if (!(this instanceof SQLWarning)) {
            if (DriverManager.getLogWriter() != null) {
                printStackTrace(DriverManager.getLogWriter());
                DriverManager.println("SQLState(" + SQLState + ")");
            }
        }
    }

    private static final long serialVersionUID = 2135244094396331484L;
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...