In JAVA we have two types of exceptions they are predefined exceptions and user or custom defined exceptions.
1. Predefined exceptions are those which are developed by SUN micro system and supplied as a part of JDK to deal with universal problems. Some of the universal problems are dividing by zero, invalid format of the number, invalid bounce of the array, etc.
exceptions are divided into two types. They are asynchronous exceptions and synchronous exceptions.
Asynchronous exceptions are those which are always deals with hardware problems. In order to deal with asynchronous exceptions there is a predefined class called java.lang.Error. java.lang.Error class is the super class for all asynchronous exceptions.
Synchronous exceptions are one which always deals with programmatic errors. In order to deal with synchronous exceptions we must use a predefined class called java.lang.Exception class.
java.lang.Exception is the super class for all synchronous exceptions. Synchronous exceptions are divided into two types. They are checked exceptions and unchecked exceptions.
An exception is an object which occurs at run time which describes the nature of the message. The nature of the message can be either system error message or user friendly message.
How an EXCEPTION OCCURS in a java Run Time Environment
User friendly messages are understand by normal user effectively hence our program is robust.
java.class.Object: super class for all JAVA class's.
Syntax for exceptional handling:
In order to handle he exceptions in JAVA we must use the following keywords. They are try, catch, finally, throws and throw.
try { Block of statements which are to be monitored by JVM at run time (or problematic errors); } catch (Type_of_exception1 object1) { Block of statements which provides user friendly messages; } catch (Type_of_exception2 object2) { Block of statements which provides user friendly messages; } . . . catch (Type_of_exception3 object3) { Block of statements which provides user friendly messages; } finally { Block of statements which releases the resources; }
Try block:
Catch block:
Finally block:
For example:
class Ex1 { public static void main (String [] args) { try { String s1=args[0]; String s2=args[1]; int n1=Integer.parseInt (s1); int n2=Integer.parseInt (s2); int n3=n1/n2; System.out.println ("DIVISION VALUE = "+n3); } catch (ArithmeticException Ae) { System.out.println ("DONT ENTER ZERO FOR DENOMINATOR..."); } catch (NumberFormatException Nfe) { System.out.println ("PASS ONLY INTEGER VALUES..."); } catch (ArrayIndexOutOfBoundsException Aioobe) { System.out.println ("PASS DATA FROM COMMAND PROMPT..."); } finally { System.out.println ("I AM FROM FINALLY..."); } } };
Throws block: This is the keyword which gives an indication to the calling function to keep the called function under try and catch blocks.
Syntax:
<Return type> method name (number of parameters if any) throws type of exception1,type of exception2,........type of exception;
Write a JAVA program which illustrates the concept of throws keyword?
Answer:
(CALLED FUNCTION) package ep; public class Ex2 { public void div (String s1, String s2) throws ArithmeticException, NumberFormatException { int n1=Integer.parseInt (s1); int n2=Integer.parseInt (s2); int n3=n1/n2; System.out.println ("DIVISOIN = "+n3); } }; (CALLING FUNCTION) import ep.Ex2; class Ex3 { public static void main (String [] args) { try { String s1=args [0]; String s2=args [1]; Ex2 eo=new Ex2 (); eo.div (s1,s2); } catch (ArithmeticException Ae) { System.out.println ("DONT ENTER ZERO FOR DENOMINATOR"); } catch (NumberFormatException Nfe) { System.out.println ("PASS INTEGER VALUES ONLY"); } catch (ArrayIndexOutOfBoundsException Aioobe) { System.out.println ("PASS VALUES FROM COMMAND PROMPT"); } } };
In JAVA there are three ways to find the details of the exception. They are using an object of java.lang.Exception class, using public void printStackTrace method and using public string getMessage method.
i. Using an object of java.lang.Exception: An object of Exception class prints the name of the exception and nature of the message.
For example:
try { int x=Integer.parseInt ("10x"); } catch (Exception e) { System.out.println (e); // java.lang.NumberFormatException : for input string 10x } name of the exception nature of the message
ii. Using printStackTrace method: This is the method which is defined in java.lang.Throwable class and it is inherited into java.lang.Error class and java.lang.Exception class. This method will display name of the exception, nature of the message and line number where the exception has taken place.
For example:
try { ......; int x=10/0; ......; } catch (Exception e) { e.printStackTrace (); // java.lang.ArithmeticException : / by zero : at line no: 4 } name of the exception nature of the message line number
iii. Using getMessage method: This is also a method which is defined in java.lang.Throwable class and it is inherited into both Error and Exception classes. This method will display only nature of the message.
For example:
try { ......; int x=10/0; ......; } catch (Exception e) { System.out.println (e.getMessage ()); // / by zero } nature of the message