Showing posts with label throw Statement. Show all posts
Showing posts with label throw Statement. Show all posts

Friday, 5 June 2015

Java Exception - throw Statement

It is possible for your program to throw an exception/error explicitly, using the throw statement.

The general form of throw is shown here:

throw ThrowableInstance;

Here, ThrowableInstance must be an object of type Throwable or a sub-class of Throwable.

Simple types, such as int or char as well as non-Throwable classes, such as String and Object, cannot be used as exceptions.

There are two ways you can obtain a Throwable object:
  • Using a parameter into a catch clause.
  • Creating one with the new operator.