All IT Courses 50% Off
QA Tutorials

Exception handling

Basically exception handling in the Java can be defined as an object which will describe an exceptional condition which has been occurred in a piece of code. When an exception condition arises object representing that exception will be created and thrown in the method which will cause the error.

Java exceptional handling will be managed by the five keywords which includes: try, catch, throw, throws and finally.

Program statements which we want to monitor for the exception will be contained within the try block, if an exception occurs within try block it is thrown.

Our code can catch exception and handle some it in some of the rational manner, syntax of exception handling is:

   // block of code which is to monitor errors
Catch(exceptiontype1 exOb)
   // exception handler for exceptiontype1
Catch (exceptiontype2 exOb)
  //exception handler for exceptiontype2
  //block of code which is to be executed
In this exception type will be the type of exception which has occurred.

Exception types

In this exception types are subclass of the built-in class throwable, this is at the top of the exception class. Throwable is been classified as exception and error.


exception error

Using try and catch:

Default exception handler is provided by the run time system. It is useful for the debugging the program in which exception is handled. This will provide two benefits: first- it will fix the errors and second- it will prevent the program which is stopped running and print a stack trace whenever an error will be occurred

Let us see an example for this which process arithmetic exception generated by division by zero

Class Exc2
Public static void main(string args[])
Int d,a;
Try {
d = 0;
A = 42 / d;
System.out.println(“this will not be printed”);
} catch (arithmeticexceptoin e)   {
System.out.println(“division by zero”);
System.out.println(“after catch statement”);


Division by zero
After catch statement

Once the exception is been thrown, program will control the transfers out of the try block into the catch block. Catch is also not called so the execution never returns to the try block from catch thus this line will not be printed. Once the catch statement is executed program control continues with next line in the program which follows entire try and catch mechanism.

Try and catch statement will form a unit. Scope of the catch clause will be restricted to those statements which is specified by the preceding try statement. Catch statement will not catch an exception thrown by another try statement.

Let us see another program where each iteration of the for loop will obtain two random integers these will be divided by each other and result is used to divide the value 12345

Import java. util. Random;
Public static void main(string args [])
Int a=0, b=0, c=0;
Random r = new Random ();
For ( int i=0; i<32000; i++ ) {
Try {
A=12345/ (b/c);
System.out.println(“division by zero”);
System.out.println(“a “+ a);


  1. What is exception handling?
  2. With program explain catch and try?

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Check Also
Back to top button

Get Python Course
worth 499$ for FREE!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys Blog will use the information you provide on this form to be in touch with you and to provide updates and marketing.