public class Test { String msg = null; public void show(){ try { try { System.out.println(10/0); } catch(ArithmeticException e) { msg = e.getMessage(); //Line 1 } try { System.out.println("IITM".charAt(5)); } catch(StringIndexOutOfBoundsException e) { msg = e.getMessage(); //Line 2 } } finally { System.out.println(msg); } } public static void main(String[] args) { Test obj = new Test(); obj.show(); } }
Question
public class Test { String msg = null; public void show(){ try { try { System.out.println(10/0); } catch(ArithmeticException e) { msg = e.getMessage(); //Line 1 } try { System.out.println("IITM".charAt(5)); } catch(StringIndexOutOfBoundsException e) { msg = e.getMessage(); //Line 2 } } finally { System.out.println(msg); } } public static void main(String[] args) { Test obj = new Test(); obj.show(); } }
Solution
The given code is written in Java.
Step 1: The code defines a public class named "Test".
Step 2: Inside the class, there is a String variable named "msg" which is initialized to null.
Step 3: The class has a public method named "show".
Step 4: Inside the "show" method, there is a try-catch block.
Step 5: Within the inner try block, there is a statement that tries to divide 10 by 0, which will throw an ArithmeticException.
Step 6: The catch block catches the ArithmeticException and assigns the exception message to the "msg" variable at Line 1.
Step 7: After the first catch block, there is another try-catch block.
Step 8: Within this inner try block, there is a statement that tries to access the character at index 5 of the string "IITM", which will throw a StringIndexOutOfBoundsException.
Step 9: The catch block catches the StringIndexOutOfBoundsException and assigns the exception message to the "msg" variable at Line 2.
Step 10: Finally, outside the try-catch blocks, there is a finally block.
Step 11: The finally block prints the value of the "msg" variable.
Step 12: The main method creates an object of the Test class and calls the show method on that object.
Similar Questions
Select the correct answerWhat is the output of the below Java code with Exceptions?public class ExceptionTest2 { public static void main(String[] args) { try { int ary[] = { 10, 20, 30 }; int tempt = ary[4]; } catch (ArrayIndexOutOfBoundsException e1) { System.out.println(e1.getMessage()); } catch (Exception e2) { System.out.println("Some exception"); } }}OptionsIndex 4 out of bounds for length 3Some exceptionNo exception occursIndex 4 out of bounds for length 3 Some exception
rrect answerWhat will be the output of the following Java code? class Name { public static void main(String args[]) { try { int ct1 = 0; int ct2 = 5; int ct3 = ct1 / ct2; System.out.print("Hello"); } catch(Exception e) { System.out.print("Codetantra"); } } }
Given1. class Test{2. public static void main(String args[]){3. try{4. new Test().meth();5. }catch(ArithmeticException e){6. System.out.print("Arithmetic");7. }finally{8. System.out.print("final 1");9. }catch(Exception e){10. System.out.print("Exception");11. }finally{12. System.out.print("final 2");13. }14. }15. 16. public void meth()throws ArithmeticException{17. for(int x=0;x<5;x++){18. int y = (int)5/x;19. System.out.print(x);20. }21. }22. }What is the output?*Arithmetic final 2Arithmetic final 1ArithmeticCompilation fails.Exception final 2
the correct answerWhat will be the output of the following Java program? class CT { public static void main(String args[]) { try { int x, y; y = 0; x = 5 / y; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } } }
What will be the output of the following Java program?class exception_handling { public static void main(String args[]) { try { System.out.print("Hello" + " " + 1 / 0); } catch(ArithmeticException e) { System.out.print("World"); } } }a) Hellob) Worldc) HelloWorldd) Hello World
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.