Could you write your own catch method pair with a try block

Could you write your own catch method pair with a try block? Study the following codes with to and catch structure, any error in the code? If there are no error in the code, the citizen (user) entering 12000 annual income. What will be the O/P

Solution

1)yes,we can write our own catch method pair with a try block.we can write our own user defined exceptions.

2)in the beginning of the code KB undefined .we can get compiletime error(kb unresoled type)

in the try block you can get compile time error after throw statement bcoz after throw statement no statements executed.it quickly creates exception object and it thrown. if proper catch statement not there handling that exception you got runtime exception and program will abnormally terminated.

3)if there are no errors in the code it comes try block after executing the throw statement it comes catch statement and it checks proper handling or not if it is proper execute catch block statements.

o/p

enter your name
david
enter your monthly income without your cents
12000
citizen name   david   java.lang.Exception: qualified for ssi supplement

package com.prt.test;

import java.util.Scanner;


public class Change{
  
   public static void main(String[] args) {
      
       //if scanner is not added it will not compile.itis not added while
       //reading the variables (incamt,citizenname)u can get compile time errors.
       Scanner KB=new Scanner(System.in);
       String citizenName;
       String s1=\"enter your name\";
       String s2=\"enter your monthly income without your cents\";
       int incAmt;
       int lowInc=13500;
       System.out.println(s1);
      
      
       citizenName=KB.nextLine();
       System.out.println(s2);//compiletime error:kb cannot resolve
       incAmt=KB.nextInt();//KB cannot be resolved
       try
       {
          
           if(incAmt<lowInc)
           {
               throw new Exception(\"qualified for ssi supplement\");
               //System.out.println(\"not qualified for ssi supplement\");//compile time error unreachable code
              
              
              
           }
      
       }catch(Exception assignSSI)
       {
          
           System.out.println(\"citizen name\\t\"+citizenName+\"\\t\"+assignSSI);
       }
      
      
   }
  
  
}

output

enter your name
david
enter your monthly income without your cents
12000
citizen name   david   java.lang.Exception: qualified for ssi supplement

 Could you write your own catch method pair with a try block? Study the following codes with to and catch structure, any error in the code? If there are no erro
 Could you write your own catch method pair with a try block? Study the following codes with to and catch structure, any error in the code? If there are no erro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site