Exception Handling try catch

Exception Handling try catch


Exception Handling try catch

Summary: "Exception Handling try catch" shows the importance of handling exceptions in Java.

Let us write one example of code, to see the weakness and robustness of exception handling.

1. Exception Unhandled

The following program explains the drawback of not handling the exception.


Exception Handling try catch

Output screen of ExceptionNotBothered.java of Exception Handling try catch

To compile the above code, the compiler does not have any objection as the code follows perfect syntax. But the runtime environment comes with a problem to divide 100 with 0. As division by zero is undefined, the JRE terminates the execution as it cannot judge what to do with z = x/y. But OK 1 is printed because it comes before the problem arises and not OK 2 and OK 3 as they occur after the problem. As Java is a friendly language, it informs what has happened, with a message, while terminating the remaining part of execution. Observe the above screen shot, it displays class ArithmeticException. Every problem in Java is represented by a class. The problem of division by zero is represented by the class java.lang.ArithmeticException.

2. Exception Handled

Following program includes a small change in the previous code, just with try-catch blocks. With this small change, a great affect is achieved, the program prints OK 2 and OK 3 apart also OK 1.

Observe the usage of try-catch blocks in the code.


Exception Handling try catch
Output screen of ExceptionBothered.java of Exception Handling try catch

Let us see where to insert try-catch blocks, when to insert and how to insert; this is very essential to a programmer. First identify, which statements, in the code, may give problem to the user at runtime. How to find? Is there any formula like (a+b)2? No such formula exists. Whenever, your code asks for some input from the user, think there may be a problem. Separate those statements and place them in try block. That is, the statements of try block may throw an exception, if user gives wrong values. After try , write a catch block that handles the exception. Provide a successful exception handler as parameter to catch block. Here, the successful handler is ArithmeticException . The programmer should be aware of the problems and the exception classes that can handle. Now do not bother much, this you get as you go ahead in the tutorial topicwise.

Remember, the catch block statements are executed only when problem arises, else simply catch is ignored (not executed); it becomes a normal program.

What is exception handling?

The process of handling the exception thrown by the Java Runtime Environment, while the program is being executed, is known as exception handling. The advantage of exception handling is the whole program is executed even if an exception arises in execution(of course, by keeping pending those statements that caused the exception).

Leave a Reply

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