You are on page 1of 6
EXCEPTION HANDALING Runtime errors in java are known as exceptions ‘= System error msg are those which are unable to understand by end user or client ‘= User frienally msg are those which are understandable by end user or client, 19s. mechanism of converting system errr msg into user friendly ms9. + srors are to types Compile time And Runtime errors + Compile time errors are those which are occuring because of poor understanding of the programming knowlege. *+ Run time errors are those wihieh are occurring ina program when the user inputs invalid data. ‘Types of Exception: * Predefined Exceptions = User Defined Exceptions, Dern: 1L. Predefined exceptions are those which are developed by SUN Micro systems and suppliad as a part of JDK to deal with Universal problems ..Some of the universal problems are dividing by zara, invalid format of numbers,nvalic bounce of arryet exceptions Predefined exceptions Asynchronous exceptions} synchronous exceptions| (hardware problems) (programmatic errors) [Uncheck exceptions| (compile time error) (run time error) Class not found Interface not found Predefined Exceptions are divided into two types They are Asynchronous exceptions and Synchronous exceptions. Asynchronous exceptions are those which are alvays deals with hardware problem. In orcer to deal with asynchronous exceptions there is @ predefined class called java.lang.Error java.lang.error class is super class forall axynchronous exceptions, —Synchronous exceptions are those 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 isthe super class forall synchronous exceptions are divided into two types....They are Checked ‘exceptions and Unchecked exceptions. 1. A checked exceptions are those which are always deals with compile time errors regarding to class not found and interface not found 2. Unchecked exceptions are those which are always deals with programmatic runtime errors such as -AvithmeticException, NumberFormatException , ArtayindexutOfBoundException,ete ‘An exception is an object which occurs at run time which describes the nature of the messageThe nature of the message can be either system error message or user friendly message. How an EXCEPTION OCCURS in a java RUNTIME ENVIRONMENT Relevent [javaang. Throwable output ee SI) la JAVA APL claput_} ava fy] i a (6. (6.21 aney Program jJavalang.Error | java.langException relevent input [1] (9) (8) m ‘Sub classes for ‘example System error Sub classes | Arithmetic Exception, messages NumberForm Exception [10] etc. 1Tser friendly messages [Exception handling] 4. Whenever we pass irrelevant input o a JAVA program, JVM cannot process the ivlevant input Since JVM is unable to process by user input, hence it can contact to JRE for getting an appropriate exception class. IRE contacts to java.lang.Throwable for finding what type of exception itis. java.lang Throwable decides wnat type of exception its and pass the message to JRE. 5, JRE pass the type of exception to JAVA API 6. [6.1 86.2}from the JAVA API either java.lang Error class or java lang Exception class will found an appropriate sub class exception. 7. Either java lang Error class or java lang Exception class gives an appropriate exception class to JRE 8, JRE will give an appropriate exception class to JVM 9. JVM will create an object of appropriate exception class which is obtained from JRE and It generates system error message 10. In order to make the program very strong(robust) JAVA programmer must convert the system error messages into se friendly messages by using the concept of exceptional handling User frienaly message are understand by normal user effectively hence our program is robust, HIERARCHY chart of EXCEPTIONAL HANDLING java.lang.object | java.lang.Throwable java.lang.Error (sub class's) java.lang.Exception (sub class’s) ;— StackoverFlowError —|_, arithmeticException +—> NumberFormatException }-—> FileNorFoundException — ArrayindexofBoundsException ‘Syntax for exceptional handaling: Tn order to handle the exceptions in JAVA we must use the following keywords They are trycatch,throw and throw. try < Block of statenents which are to be monitored by JVM at run time (or problenatic errors) ; 3 catch (Type_of exception! object) 4 Block of statenents which provides user friendly messages; 3 catch (Type_of exception? object2) < Block of statenents which provides user friendly messages; > catch (Type_of exception? object3) 4 Block of statenents which provides user friendly messages; 3 Finally < Block of statenents which releases the resources; > Try block: 1 caten 1 ‘This is the block in which we write the block of statements which are to be monitored by JVM at run time ie. try block must contain those statements which causes problems at run time, 1 any exception is taking place the control will be jumped automatically to appropriate catch block any exception is taking place in try black execution wil be terminated and the rest ofthe statements in ty block will not be executed at all and the contro will goto catch block For every try block we must have at least one catch block. Its highly recommended to write in’ number of catch's ‘or in number of problematic statements block: ‘This is used for providing user friendly messages by catching system error messages. In the catch we must declare an object of the appropriate execution class and ‘whenever the appropriate situation taking place. il be internally referenced JVM. Ife writen’ number of catch’ asa part of JAVA program then only ane catch will be executing at any point. cum statement After executing appropriate catch block even if we user to ty block the catch block the control never goes Finally bolck ‘This is the block which is executing compulsory whether the exception is taking place or not. This block contains same statements which releases the resources which are obtained in ty block (resources are opening files, opening databases, etc) Writing the finaly block 's optional For Example: class Ext < public static void main (String [] args) < t < s ry ‘ering si=args[e]; String s2=args[1]; nt nl-Integer.parseInt (s1); int n2=Integer.parseInt (#2); int n=ni/n2} System.out.println ("DIVISION VALUE = "#n3); 3 4 System.out.println ("DONT ENTER ZERO FOR DENOMINATOR. + atch (Arithmeticexception Ae) catch (NumberFormatexception Nfe) { System.out.printLn ("PASS ONLY INTEGER VALUES. 3 catch (ArrayIndexutofBoundsException Aisobe) < System.out.printn ("PASS DATA FROM COMMAND PROMPT. 3 Finally q System.out.println ("I AM FROM FINALLY..."); 3 “This s the keyword which gives an indication to the calling function to keep the called function under try and catch block. Syntax ‘Return type> method name (number of parameters if any) throws type of exception! , type pf exception 2,...ype of exception; wa java prog, which will ilustrates the concept of throws keyword? Answer (CALLED FUNCTION) package ep; public class x2 q public void div (String sl, String s2) throws ArithmeticException, NunberFornatExcept~ q Int nl=Integer.parseInt (51); int n2=Integer.parseInt ($2); int n3-ni/n2; System.out.println ("DIVISOIN = "+n3) ; 3 38 (CALLING FUNCTION) import ep. Ex2; class Ex3 < public static void main (String [] args) { try 4 String sizargs [0]; String s2=args [1]; Ex2 eo-new x2 (); eo.div (s1,82); Zatch (ArtthacticException Ae) Systen-out.printtn (COONT ENTER ZERO FOR DENOMINATOR"); en (NumberFormatException Nfe) Systen-out.printtn (YPA3S INTEGER VALUES OLY) taten (ArrayIndexoutortcundstiception Aioobe) Systen-out-printtn (OPASS VALUES FROM COMMAND PROKFT®); } vs Number of ways to find details of the exception: —n java there are three ways to find the details of the exception They are using an object of java tang Exception clas, using. public vold printStackTrace() method and using public string getMessage() method. 11, Using an object of javalang.Excey of the message. on: An object of Exception class prints tne name of the exception and nature For example: ty int > caten(Fxception e) fl System.out printin(2) java lang NumberFormatException : for inputs string 10x , Integer parselnt("10x" 2 Using printStackTrace() method: This isthe methood which is defined in java.lang Throwable class and itis, inherited into java.lang Error class and java.lang.Exception class. This method will display name of the n.nature of the message and line number where the exception has taken place

You might also like