Handling Multiple Exceptions

Home  »  Java  »  Advance Java  »  Java Exception Handling  »  Handling Multiple Exceptions


The following example shows how to handle multiple exceptions. For example, if the user enters a zero in the second text field and Java tries to divide the first number by the zero, an ArithmeticException exception is raised. So, what if there are two exceptions to deal with i.e: the NumberFormatException and the ArithmeticException. This means there are multiple exceptions to be handled.

The good news is that there is no limit to the number of catch blocks. The catch blocks can be generated for any exception that the program generates.

The below program show how to handle multiple exceptions in java programming.





C:\>jdk1.4\bin>javac HandMulExcepApp.java
C:\>jdk1.4\bin>appletviewer HandMulExcepApp.html

The actionPerformed() in the above code uses two catch program blocks, one each for the NumberFormatException and ArithmeticException exceptions.

The ArithmeticException is raised when a number is divided by zero. In above Diagram the first number i.e. 10 is divided by the second number i.e. 0. This gives a message i.e. "Division by 0, this is NOT ALLOWED in Math".


Download Complete Program

  -    -  HTML File



Java Exception Handling


Catching Java Exceptions Catching a RunTime Exception
Handling Multiple Exceptions The finally Clause
Creating User-defined Exceptions




No comments:

Post a Comment