Sunday, September 6, 2009

What is user-defined exception in java ?

Answer : User-defined expections are the exceptions defined by the application developer which are errors related to specific application. Application Developer can define the user defined exception by inherite the Exception class as shown below. Using this class we can throw new exceptions.

Java Example :

public class noFundException extends Exception {
}

Throw an exception using a throw statement:

public class Fund {

...
public Object getFunds() throws noFundException {

if (Empty()) throw new noFundException();
...

}
}

User-defined exceptions should usually be checked.

No comments:

Post a Comment