Monday, September 26, 2011

Difference between Throw,Throws and Throwable?

The keyword throw is used to throw user defined exceptions and it requires a single argument(a throwable class object)

ex: throw new XYZException("Test");

The keyword throws is used in method signatures to declare that, this method could possibly throw an exception.

ex: public void test() throws SQLException {
}

Throwable is an interface that the Exception class implements and an interface that all user defined class would implicitly implement to ensure that they have exception like behavior..

The throw keyword is used to explicitly throw an exception.

The throws keyword is used to declare what types of exceptions are thrown by a method.

The Throwable class is the superclass of all errors and exceptions in Java.

No comments:

Post a Comment