EXCEPTION HANDLING ================== Part-04 ======= Nested try block: ================= ==> this can be used in distributed application development. Distributed Applications: Ex: Phonepe { transfer{ if(amount < 0){ not possible } else if(amount > balance){ transfer not possible; } else{ transfer successful; } } Phonepe ==> Bank Server Application Ex: ==== try{// outer try block st-1; st-2; try{ // inner try block st-3; st-4; // exception } catch(Exception e){ // inner catch block System.out.println(e); } } catch(Exception e) // outer catch block { System.out.println(e); } } ==> When an exception is occurred in outer try block: then, that exception is always handled by outer catch block only. ==> If an exception is occurred in inner try block, then it can be handled by inner catch block. If the inner catch block not able to handle that exception, the outer catch block can handle that exception. ============================================ Custom exceptions: ================== ==> User-defined exceptions are also called as "Custom Exceptions" Syntax: public class Your_Own_Exception_Class_Name extends RuntimeException{ // unchecked Custom Exception } public class Your_Own_Exception_Class_Name extends Exception{ // checked custom Exception } Ex: public class InvalidAgeException extends Exception{ } ==> To print the custom exception class name along with message: we can define the parameterized constructor in the Custom exception class. ex: public class InvalidAgeException extends Exception{ public InvalidAgeException(String message){ super(message); } } ==> To print the custom exception class without message, we can use default constructor. Ex: public class InvalidAgeException extends Exception{ public InvalidAgeException(){ super(); } } ============================================================ throw keyword: ============== try{ c = 5/0; // Exception // JVM throw new ArithmeticException ("/by zero"); System.out.println(c); } catch(Exception ex){ System.out.println(ex); } java.lang.ArithmeticException: / by zero Ex: try{ if(age < 0){ throw new InvalidAgeException("age < 0 is invalid"); } } catch(Exception ex) { System.out.println(ex); } ========================================== throws keyword: =============== can be used to propagate the exception from try block to caller method. ==> throws can always use in method signature. Ex: // can propagate any exception void m1() throws Exception{ l1; l2; l3; } // propagating the specific exception void m1() throws NullPointerException{ l1; l2; l3; } // to propagate an error void m1() throws Throwable{ l1; l2; l3; } =========================================== Practice with Exceptions: ========================= InvalidAmountException.java =========================== package com.ashokit.custom; public class InvalidAmountException extends RuntimeException { public InvalidAmountException(String message) { super(message); } } InsufficientBalanceException.java ================================== package com.ashokit.custom; public class InsufficientBalanceException extends RuntimeException{ public InsufficientBalanceException(String message) { super(message); } } BankAccount.java: ================== package com.ashokit.bank; import com.ashokit.custom.*; public class BankAccount { private double balance; public BankAccount(double balance) { this.balance = balance; } public void deposit(double amount) { try { if(amount <= 0) { throw new InvalidAmountException("The Given Amount <= 0, it can't be deposited."); } balance += amount; System.out.println("Balance After the Deposit:"+balance); } catch(Exception ex) { System.out.println(ex); } } public void withdraw(double amount) { try { if(amount <= 0) { throw new InvalidAmountException("The given amount <= 0, can't withdraw."); } if(amount > balance) { throw new InsufficientBalanceException("The given amount > balance, can't withdraw."); } balance -= amount; System.out.println("Account Balance after withdraw:"+balance); } catch(Exception ex) { System.out.println(ex); } } } MainClass.java: =============== package com.ashokit.main; import com.ashokit.bank.*; public class MainClass { public static void main(String[] args) { BankAccount account = new BankAccount(55000.0); // account.deposit(-1000); account.deposit(10000.0); // account.withdraw(-1000.0); // account.withdraw(70000.0); account.withdraw(25000.0); } } ==================================================================== ==> When a parent class method with propagation of an Exception is overriding in the child class, then the child class method with or without propagation of exception is possible. ==> When a parent class method with propagation of an Exception is overriding in the child class, then the child class method want propagate the exception means that exception must be the same level. parent class method propagating Unchecked exception ==> child class method also propagate uncheck exception only. package com.ashokit.demo; public class ClassA { void m1() throws NullPointerException{ // logic with three statements } } class ClassB extends ClassA{ @Override void m1() throws ArrayIndexOutOfBoundsException{ // logic with 10 statements; } } ========================================== try with resources: =================== try with parameters auto close parameters try(Connection conn = DriverManager.getConnection(url, un, pwd)){ // code } catch(Exception ex) { // code }