Exceptions in Java

Overview of an Exception:

An exception is an object of an exception type: a subclass of class Throwable. It is used to signal and describe an abnormal situation during program execution. The evaluation of an expression or the execution of a statement may terminate abruptly by throwing an exception, either by executing a throw statement or by executing a primitive operation, such as assignment to an array element, that may throw an exception.

A thrown exception may be caught in a dynamically enclosing try-catch statement.If the exception is not caught, then the entire program execution will be aborted, and information from the exception will be printed on the console. What is printed is determined by the exception's toString method.

There are two kinds of exception types: checked (those that must be declared in the throws-clause of a method or constructor;and unchecked (those that need not be). If the execution of a method or constructor body can throw a checked exception of class E, then class E or a super type of E must be declared in the throws-clause of the method or constructor.

The following table shows part of the exception class hierarchy.

Declaring a Checked Exception Class

This is the class of exceptions thrown by method wdayno4. Passing a string to the constructor of the super class (that is, class Exception) causes method toString to append that string to the name of the exception.

class CheckedExceptionDemo extends Exception {

public CheckedExceptionDemo(String wday) {
super("Illegal weekday: " + wday)
}
}































RELATED POST:

INTERFACES IN JAVA

No comments:

Post a Comment