[!INDEX]

  1. Definition ( 3 points ) + divide /0 mt().
  2. Example code arithmetic ex in calling mt()
  3. ex. handle in calling class + try-catch ( above code )
  4. ex. handle in caller class + try-catch ( above code )
  5. exception flow theory ( 5 points )
  6. throw keyword ( 8 points )
  7. Syntax of throw.
  8. Throwing r/t e/x, + no try-catch + error ( student age )
  9. above code + try-catch + error ( above code )
  10. above code + try-catch + no error ( above code )
  11. Approach

1. Definition ( 3 points ) + divide /0 mt().

[!NOTE]

  1. used to explicitly throw an exception from your code.
  2. When you use throw, you're manually creating an exception object and handing it off to the Java runtime (JVM). The thrown exception can then be caught by a try-catch block, or it will propagate up the call stack until it's caught or the program terminates.
  3. if it is not handle , jvm pass to DEH and program terminate abnormally

2. Example code arithmetic ex. in calling mt()

package package2;
public class Main1 {  
        public static void main(String[] args) {
            Main1 m1 = new Main1();
                m1.divide();
                System.out.println("hello");
        }
        void divide(){
            int a=100, b=0, c;
            c=a/b;
            System.out.println(c);
        }    
    }
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at package2.Main1.divide(Main1.java:10)
        at package2.Main1.main(Main1.java:5)

3. ex. handle in calling class + try-catch ( above code )

package package2;
public class Main1 {  
    public static void main(String[] args) {
        Main1 t1 = new Main1();
        t1.divide();
        System.out.println("hello");
    }
/* calling mt */    
    void divide(){
        try{
            int a=100, b=0, c;
            c=a/b;
            System.out.println(c);
        }catch(ArithmeticException e){
            e.printStackTrace();
        }
    }
}
//hello

4. ex. handle in caller class + try-catch ( above code )

package package2;
public class Main1 {  
    public static void main(String[] args) {
        Main1 t1 = new Main1();
        try{
            t1.divide();
        }catch(ArithmeticException e){
            e.printStackTrace();
        }
        System.out.println("hello");
    }
    void divide(){
        int a=100, b=0, c;
        c=a/b;
        System.out.println(c);
    }
}
//hello

5. exception flow theory ( 5 points )

[!NOTE]

  1. एक्सेप्शन आने पर divide mt object create करेगा और
  2. JVM usko detect karke चेक करेगा की divide mt ने exception handle करा है की nahe.
  3. if no then jvm immediatly terminate the divide method.
  4. फिर JVM us object ko , caller (main) method में pass kar deyga aur चेक करेगा की exception handle करा है की nahe if no
  5. तो JVM वो डिफ़ॉल्ट एक्सेप्शन हैंडल (DEH) को पास कर deyga और वो फिर उसको हैंडल करेगा और प्रोग्राम अब्नोर्मल्ली टर्मिनेट हो जाएगा |
  6. can use try catch , in both method , but prefers in the divide method

6. throw keyword ( 8 points )

[!NOTE]

  1. explicitly throw an exception from your code.
  2. manually creating an exception object and handing it off to the Java runtime (JVM).
  3. The thrown exception can then be caught by a try-catch block, or it will propagate up the call stack until it's caught or the program terminates.
  4. create and throw both checked and unchecked exceptions.
  5. Single exception at a time
  6. Used in methods
  7. prefer for unchecked exception
  8. only using this still make the program terminate abnormally.

7. Syntax of throw:

throw new ExceptionType("Error message");

8. Throwing r/t e/x, + no try-catch + error ( student age )

public class Main1 {  
    int age;
    public static void main(String[] args) {        
       System.out.println("age is entered");
        int age = 17 ;
        Checkage ca = new Checkage();
        ca.studentage(age);
        System.out.println("code flow ok in parent clas ");
    }
}

class Checkage{
    void studentage(int x){
        if(x<18){
            throw new ArithmeticException("age is less");
        }
        else{
            System.out.println("age ok");
        }
        System.out.println("code flow ok in child clas ");
    }
}
age is entered
Exception in thread "main" java.lang.ArithmeticException: age is less
        at package2.Checkage.studentage(Main1.java:17)
        at package2.Main1.main(Main1.java:10)

9. above code + try-catch + error ( above code )


public class Main1 {  
    int age;
    public static void main(String[] args) {        
       System.out.println("age is entered");
        int age = 17 ;
        Checkage ca = new Checkage();
        try{
            ca.studentage(age);
        }
        catch(ArithmeticException e)
        {
            e.printStackTrace();
        }
        System.out.println("code flow ok in parent class ");
    }
}

class Checkage{
    void studentage(int x){
        if(x<18){
            throw new ArithmeticException("age is less");
        }
        else{
            System.out.println("age ok");
        }
        System.out.println("code flow ok in child clas ");
    }
}
age is entered
java.lang.ArithmeticException: age is less
        at package2.Checkage.studentage(Main1.java:24)
        at package2.Main1.main(Main1.java:12)
code flow ok in parent clas

10. above code + try-catch + no error

public class Main1 {  
    int age;
    public static void main(String[] args) {        
       System.out.println("age is entered");
        int age = 19 ;
        Checkage ca = new Checkage();
        try{
            ca.studentage(age);
        }
        catch(ArithmeticException e)
        {
            e.printStackTrace();
        }
        System.out.println("code flow ok in parent clas ");
    }
}

class Checkage{
    void studentage(int x){
        if(x<18){
            throw new ArithmeticException("age is less");
        }
        else{
            System.out.println("age ok");
        }
        System.out.println("code flow ok in child clas ");
    }
}
age is entered
age ok
code flow ok in child clas
code flow ok in parent clas

11. Approach

[!approach]

  1. call method (studentage) of other class (Checkage) using try-catch
  2. check and throw exception in studentage

image