232. Using throw keyword in Exception Handling

Exception -

An exception is an object that signals the occurrence of an unusual event during the execution of a program. The process of creating this object (i.e. generating an exception) is called throwing an exception.

Exceptions can be thrown in two ways -

  • JVM (Java Virtual Machine) can throw exception while executing the program  (Example - ArithmeticExceptoins, ArrayIndexOutOfBoundsException etc)
  •  We can manually throw an exception based on our own conditions using throw keyword (Example - If we don't want the age below 18 years to be accepted by our application in any field say 'Age', we can verify the condition whether the age is 18 years or above and if not, we can throw our own exception Class to handle our own thrown exception)

Lets understand the usage of throw keyword for manually throwing an exception based on our business condition using an example -

I have created the below form for registering User, which wont allow Users having the age less than 18 years. If any User enters an age less than 18 years, instead of breaking the program, a message which says 'You cannot register with us' will be displayed. Please try the below form by entering the age which is less than 18 years and view the message given by the form -

The above form verifies whether the age is less than 18 years and if the age entered is less than 18 years, then the verification statement inside the program fails and an exception will be thrown manually by the program using throw statement and handled by the User defined Exception Class by giving the messages like 'You cannot register with us'  (How to create our own Exception Classes will be explained in the later posts).

As we have understood why we have to throw the exceptions manually and handle them using our own exception classes. Now as it is not possible to show the usage of throw in programs as it is used in the real time development projects like the above form, I will implement the throw statement with the predefined ArithmeticException Class to show how the throw statement is used in code for manually handling the exceptions even though we never use it with AirthmeticException Class in real time.

Syntax -

throw new exception("exception cause text");

Example for Implementing the throw statement for manually throwing the AirthmeticException -

class ThrowDemo
       public static void main(String args[])
                    throw new ArithmeticException("This is a manually thrown exception");
             catch(ArithmeticException exception)
                   exception.printStackTree( );   //Prints the exception details in detail.


We have manually thrown the exception using the throw keyword in the above code.

As the throw statement is not used for handling the ArithmeticException in real time, the above example is written for the explanation purpose only.

Lets implement throw statement for manually throwing the ArithmeticException on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class file 'ThrowDemo' with main( ) method in the existing Java Project 'Project 46' as shown below -

2. Create a try block and throw ArithmeticException manually using the throw statement as shown below -

3. Handle the exception using the catch block and print the exception details as shown below -

4. Save and Run the '' Class and observe that the manually throw exception details are displayed in the output as shown below -

Hence we can manually throw the predefined or user defined exceptions using the throw statement and handle them. But in real time, throw statement is used only to handle the user defined exceptions which are created according to the User business needs.

Please comment below to feedback or ask questions.

Checked Exceptions will be explained in the next post.

No comments: