Why does the following code output the System.out.println(\"After the method cal
ID: 3861311 • Letter: W
Question
Why does the following code output the System.out.println("After the method call");? According to my book, the program should halt after the catch, and goes back to the main method to execute what's AFTER try and catch block.public class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (RuntimeException ex) { System.out.println("RuntimeException in main"); } catch (Exception ex) { System.out.println("Exception in main"); } }
static void method() throws Exception { try { String s ="abc"; System.out.println(s.charAt(3)); } catch (RuntimeException ex) { System.out.println("RuntimeException in method()"); } catch (Exception ex) { System.out.println("Exception in method()"); } } }
Output: RuntimeException in method() After the method call Why does the following code output the System.out.println("After the method call");? According to my book, the program should halt after the catch, and goes back to the main method to execute what's AFTER try and catch block.
public class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (RuntimeException ex) { System.out.println("RuntimeException in main"); } catch (Exception ex) { System.out.println("Exception in main"); } }
static void method() throws Exception { try { String s ="abc"; System.out.println(s.charAt(3)); } catch (RuntimeException ex) { System.out.println("RuntimeException in method()"); } catch (Exception ex) { System.out.println("Exception in method()"); } } }
Output: RuntimeException in method() After the method call Why does the following code output the System.out.println("After the method call");? According to my book, the program should halt after the catch, and goes back to the main method to execute what's AFTER try and catch block.
public class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (RuntimeException ex) { System.out.println("RuntimeException in main"); } catch (Exception ex) { System.out.println("Exception in main"); } }
static void method() throws Exception { try { String s ="abc"; System.out.println(s.charAt(3)); } catch (RuntimeException ex) { System.out.println("RuntimeException in method()"); } catch (Exception ex) { System.out.println("Exception in method()"); } } }
Output: RuntimeException in method() After the method call
Explanation / Answer
Hi,
Please replace the line charAt(3) with charAt(2) if you are trying to access the third character of your string. Your program is actually generating StringIndexOutOfBoundsException error.This is because you are trying to access a string array value which does not exist.
Explanation-You have assigned a string value "abc" to string variable s. In memory the allocation begins from 0 for tge first character of the string and then the remaining characters will be stored as below-
charAt(0) -- a
charAt(1)--b
charAt(2)--c
hence your entire string ends at c and does not have anything stored at charAt(3).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.