Catching a RunTime Exception

Home  »  Java  »  Advance Java  »  Java Exception Handling  »  Catching a RunTime Exception


The following example is an advancement of the above example. This example shows how to handle exceptions. For example, the following code catches the NumberFormatException in case leaves either of the text boxes empty or enters an invalid value such as a String.





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


The actionPerformed() uses the try and the catch blocks to handle the NumberFormatException. The below Diagram shows what happens when the user leaves the text fields blank or enters an invalid numerical value. When the program gets to the first call to String.valueOf(). Java generates the NumberFormatException exception, which causes program execution to jump to the catch block. In the catch block, the program sets the display string to "Invalid Number". This is done by setting the text of the label to "Invalid Number".


Invalid numerical value being entered


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