EXCEPTION HANDLING ================== types of errors: 1) Syntax Error 2) Run time error 3) Logical Error Logical Error ==> never be detected by compiler and never be by JRE/JVM ==> The logical errors can be detected and rectify using the procedure of "Debugging" or "Unit Testing" using JUnit. ==> To handle the exception, there are 5-keywords: try catch finally throw throws try: === Ex: deposit(amount){ if amount > 0: balance += amount; else no deposit is possible; } divide(a,b){ c = a/b; Sop(c); } a = 100 b = 0/1 100/0 => error ==> run time error/exception arithmetic exception ==> The amount of code with the chances of exception can write within the try-block. ==> try block can be defined with "try" keyword. Syntax: try{ //logic with/without an exception } Ex: try{ divide(int a,int b) { int c = a/b; System.out.println(c); } } catch: ====== divide(int a, int b) { int c = a/b; // possibility of exception with b = 0 Sop(c); } ==> message: dividing a number with 0 is not possible. ==> catch ==> a keyword used to define the catch block. ==> to define the block of code which need to handle the exception which occur in try-block catch block can be used. Syntax: catch(Exception Object-name){ // logic to handle the exception } ex: try{ divide(int a, int b) { int c = a/b; // possibility of exception with b = 0 Sop(c); } } catch(Exception ex){ System.out.println("Dividing a number with 0 is not possible."); } ======================================== package first.second; import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a,b,c; System.out.println("Enter a and b:"); a = sc.nextInt(); b = sc.nextInt(); try { c = a/b; System.out.println("The Result after the division = "+c); } catch(Exception ex) { System.out.println("Dividing a number with 0 is not possible."); } } } ================================================================== finally: ======== finally ==> keyword use to define finally block. ==> finally ==> optional block. ==> when we want to make execute the block of code in your application with irrespective of an exception, we can use "finally" block. Syntax: finally{ //logic to execute with irrespective of exception } package first.second; import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a,b,c; System.out.println("Enter a and b:"); a = sc.nextInt(); b = sc.nextInt(); try { c = a/b; System.out.println("The Result after the division = "+c); } catch(Exception ex) { System.out.println("Dividing a number with 0 is not possible."); } finally { System.out.println("Application is Running..."); } } } =========================================================================== Display Exception in Catch Block: ================================= ==> three ways: 1) System.out.println(Exception-object) 2) exception-object.getMessage() 3) exception-object.printStackTrace() 1) System.out.println(Exception-object) ======================================= ==> we can display the exception class name and message package first.second; import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a,b,c; System.out.println("Enter a and b:"); a = sc.nextInt(); b = sc.nextInt(); try { c = a/b; System.out.println("The Result after the division = "+c); } catch(Exception ex) { System.out.println(ex); } } } Output: ======= Enter a and b: 100 0 java.lang.ArithmeticException: / by zero ================================================== 2) exception-object.getMessage() ================================ ==> used to display the exception with only the message package first.second; import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a,b,c; System.out.println("Enter a and b:"); a = sc.nextInt(); b = sc.nextInt(); try { c = a/b; System.out.println("The Result after the division = "+c); } catch(Exception ex) { System.out.println(ex.getMessage()); } } } Output: ====== Enter a and b: 100 0 / by zero ================================================================ 3) exception-object.printStackTrace() ====================================== stackTrace() ==> full description about an exception includes: 1) Exception Class name 2) Exception message 3) Project name 4) Package Name 5) Class Name 6) Method name 7) Position ==> line number package first.second; import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a,b,c; System.out.println("Enter a and b:"); a = sc.nextInt(); b = sc.nextInt(); try { c = a/b; System.out.println("The Result after the division = "+c); } catch(Exception ex) { ex.printStackTrace(); } } } Output: ====== Enter a and b: 100 0 java.lang.ArithmeticException: / by zero at ExceptionHandling/first.second.MainClass.main(MainClass.java:15) ========================================================================== Q-1: Dividing an integer with double and storing the result into an integer variable, what happened? A: leads Syntax Error Q-2: Do we have any exception with the given code: package first.second; import java.util.Scanner; class ClassA{ public void divide(int a, double b) { try { double c = a/b; System.out.println(c); } catch(Exception e) { e.printStackTrace(); } } } public class MainClass { public static void main(String[] args) { ClassA ca = new ClassA(); ca.divide(120, 0.0); } } Ans: No Exception here Infinity Q-3: any exception in the given code: package first.second; import java.util.Scanner; class ClassA{ public void divide(double a, int b) { try { double c = a/b; System.out.println(c); } catch(Exception e) { e.printStackTrace(); } } } public class MainClass { public static void main(String[] args) { ClassA ca = new ClassA(); ca.divide(120.0,0); } } Ans: NO Exception Infinity Q-4: try{ statement-1; // executed statement-2; // exception statement-3; // not to be executed statement-4; // not to be executed } catch(Exception e) { logic; } finally{ logic; } ==> when an exception is occurred in between the try block, the control can stop the remaining statements of try block immediately and control can jump to catch() for execution and execute the finally block after the catch() block. ================================================== Q-5: try{ statement-1; // executed statement-2; // executed statement-3; // executed statement-4; // executed } catch(Exception e) { logic; } finally{ logic; } ==> when there is no exception in the try block, then the control can stop the except() block and continue with finally block for execution. Q-6: try{ statement-1; // executed statement-2; // exception statement-3; // not to be executed statement-4; // not to be executed } catch(Exception e) { logic; } finally{ logic; } External statements; ====================================== Is it Possible to stop executing the finally block? =================================================== Yes. package first.second; import java.util.Scanner; class ClassA{ public void divide(int a, int b) { try { int c = a/b; System.out.println(c); } catch(Exception e) { e.printStackTrace(); return; } finally { System.out.println("Application is in progress.."); } } } public class MainClass { public static void main(String[] args) { ClassA ca = new ClassA(); ca.divide(100,0); } } Note: ===== In general, return can give the immediate termination from the method execution by returning some value. ==> because of return, we cannot stop the execution of finally block. package first.second; import java.util.Scanner; class ClassA{ public void divide(int a, int b) { try { int c = a/b; System.out.println(c); } catch(Exception e) { e.printStackTrace(); System.exit(0); } finally { System.out.println("Application is in progress.."); } } } public class MainClass { public static void main(String[] args) { ClassA ca = new ClassA(); ca.divide(100,0); } } ==> we can stop making the execution of finally block by defining System.exit(0).