[!INDEX]

  1. Approach ( 4 points ) + UnderAgeExceptions
  2. custom checked e/x + error + throw + without handle( not compile ) + above code
  3. custom checked e/x with error + try-catch + above code
  4. r/T e/X with throws( compiler not detect the issue) + not handle + above code
  5. r/T e/X + try-catch + throw + above code
  6. statement after throw ( error) +throw + try-catch + above code

1. Approach ( 4 points ) + UnderAgeExceptions

[!approach]

  1. create custom class with its ct
  2. extends to exception(ct,rt)
  3. pass super to ct
  4. another mt check condtion and throw exception

2. custom checked e/x + error + throw + without handle( not compile ) + above code

package package2;
class UnderAgeExceptions extends Exception{
    UnderAgeExceptions(){
        super();//super will provide description to DEH
    }
    UnderAgeExceptions(String message){
        super(message);
    }
}
public class Main1 {  
    public static void main(String[] args) {
        int age = 17;
        if(age<18){
            throw new UnderAgeExceptions(); // for no argument c/T
        }
    }
}
Unhandled exception type UnderAgeExceptionsJava(16777384)

3. custom checked e/x with error + try-catch + above code

package package2;
class UnderAgeExceptions extends Exception{
    UnderAgeExceptions(){
        super();
    }
    UnderAgeExceptions(String message){
        super(message);
    }
}
public class Main1 {  
    public static void main(String[] args) {
        int age = 17;
        try{
            if(age<18){
                throw new UnderAgeExceptions("EXCEPTION **: "); // parameterized
            } 
        }catch( UnderAgeExceptions e){
                e.printStackTrace();
            }
        System.out.println("hiii");      
    }
}
package2.UnderAgeExceptions: EXCEPTION **: 
        at package2.Main1.main(Main1.java:17)
hiii

4. r/T e/X with throws( compiler not detect the issue) + not handle + above code

package package2;
class UnderAgeExceptions extends RuntimeException{
    UnderAgeExceptions(){
        super();
    }
    UnderAgeExceptions(String message){
        super(message);
    }
}
public class Main1 {  
    public static void main(String[] args) throws UnderAgeExceptions{
        int age = 17;
        if(age<18){
            throw new UnderAgeExceptions("EXCEPTION RT **: "); // parameterized
        }  
        System.out.println("hiii");      
    }
}
Exception in thread "main" package2.UnderAgeExceptions: EXCEPTION RT **: 
        at package2.Main1.main(Main1.java:15)

5. r/T e/X + try-catch + throw + above code

package package2;
class UnderAgeExceptions extends RuntimeException{
    UnderAgeExceptions(){
        super();
    }
    UnderAgeExceptions(String message){
        super(message);
   }
}
public class Main1 {  
    public static void main(String[] args) {
        int age = 17;
        try{
            if(age<18){
                throw new UnderAgeExceptions("EXCEPTION **: "); // parameterized
            }  
        }catch( UnderAgeExceptions e){
            e.printStackTrace();
        }
        System.out.println("hiii");      
    }
}
package2.UnderAgeExceptions: EXCEPTION **: 
        at package2.Main1.main(Main1.java:16)
hiii

6. statement after throw ( error) +throw + try-catch + above code

package package2;
class UnderAgeExceptions extends RuntimeException{
    UnderAgeExceptions(){
        super();
    }
    UnderAgeExceptions(String message){
        super(message);
    }
}
public class Main1 {  
    public static void main(String[] args) {
        int age = 17;
        try{
            if(age<18){
                throw new UnderAgeExceptions("EXCEPTION **: "); // parameterized
                System.out.println("no statement after throw");
            }  
        }catch( UnderAgeExceptions e){
            e.printStackTrace();
        }
        System.out.println("hiii");      
    }
}
Unreachable codeJava(536871073)

image