Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Question is the following try catch pair statements legal? tryt if (time 29

ID: 3601867 • Letter: J

Question

Java Question

is the following try catch pair statements legal? tryt if (time 29) catch (Exception time is over ) throw new Exception ("Time is exceeded!"); System.out.printin "Leaving try block.") System.out.printin ("Exception: "+ time is_over.getMessage)): ANS: circle Yes or No Could you write our own catch method pair with a try block? ANS (1) ANS (: circle Yes or No 49 What O/P is produced by the following code? int waitTime= 46; try System.out.println ("Try block entered."): if (wait Time > 30) throw new Exception (Time limit exceeded."): System.out.println ("Leaving try block.") catch (Exception e) System.out.println ("Exception:” + e.get Message()); System.out,println ("After catch block"): ANS:

Explanation / Answer

47) Ans:- Yes

The try catch statement in the question is legal. Because we can see the try block is throwing an exception when time value is greater than 29 which gets captured in the catch block. catch block is printing the exception message which in this case is "Exception:Time is exceeded".

You can verify the same by using sample program like below.

public class Main

{

  

public static void main(String[] args) {

int time = 101010101;

  

try{

if(time > 29) throw new Exception("Time is exceeded");

System.out.println("Leaving try block");

}

catch(Exception time_is_over){

System.out.println("Exception:"+time_is_over.getMessage());

}

}

}

48) Ans:- Yes

We can write our own catch method pairing with try block. To do that we have to use throws block from a method to throw an user defined exception and the same has to be handled in catch block. Below is code template showing the same.

We can see MyException is a class which has a method throwing UserDefinedException when the value of a is greater than 10. In other class MainProgram we can see MyException class is defined and inside try block it is calling method1 in MyException class with value 100. This statement triggers UserDefinedException which gets captured in the catch block. Note that this is just a template to show the usage but not the actual code.

class MyException{

public void method1(int a) throws UserDefinedException {

String mesg = "Error message";

if(a > 10){

throw new UserDefinedException(mesg);

}

}

}

public class MainProgram {

public static void main(String [] args) {

MyException c = new MyException();

  

try {

c.method1(100);

}catch(UserDefinedException e) {

System.out.println("Exception:" + e.printStackTrace());

}

}

}

49) Ans:-

Exception:Time limit exceeded

After catch block  

The code produces the above message. Because exception gets triggerred when waitTime value is greater than 30. Since waitTime is initalized to 46, the exception get thrown which is captured in catch block. Then the following System.out.println message gets executed which prints "After catch block". You can verfiy the same with the code used below.

Code:-

public class Main

{

  

public static void main(String[] args) {

int waitTime = 46;

try{

System.out.println("Try block entered");

if(waitTime > 30)

throw new Exception("Time limit exceeded");

System.out.println("Leaving try block");

}

catch(Exception e){

System.out.println("Exception:"+e.getMessage());

}

System.out.println("After catch block");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote