Results 1 to 4 of 4
  1. #1
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    106
    Rep Power
    0

    Default Catch block should not have been triggered?

    I have a simple try-catch-finally block program:
    Java Code:
    package sam.main;
    
    public class Main{
    	
    	public static void main(String[] args) {
    		
    		System.out.println("a");//line 13
    		try {
    			System.out.println("Now in try");
    			System.out.println("b");
    			throw new IllegalArgumentException();
    		}catch(RuntimeException e) {
    			System.out.println("Now in catch");
    			System.out.println("c");
    		}finally {
    			System.out.println("d");
    		}
    		System.out.println("e");
    
    	}//end of main method	
    }
    The output is this:

    a
    Now in try
    b
    Now in catch
    c
    d
    e

    What I don't understand is why the catch block was triggered (evidently since lines 13 and 14 printed their strings).
    How does the simple print statement on line 10 trigger an IllegalArgumentException?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Catch block should not have been triggered?

    What is in e when the catch block is triggered? Print it out to see what exception was caught.
    Better yet call the printStackTrace method to get the stack trace to see where the exception came from.

    Note: Always call the printStackTrace method in a catch block.
    Last edited by Norm; 09-30-2019 at 09:58 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Catch block should not have been triggered?

    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    8

    Default Re: Catch block should not have been triggered?

    You throw an IllegalArgumentException on line 11. That is a subclass of RuntimeException so it is caught.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Try-Catch block issues
    By Uchiha in forum New To Java
    Replies: 4
    Last Post: 10-30-2013, 02:26 PM
  2. Try Catch Block
    By Jackbower in forum New To Java
    Replies: 3
    Last Post: 05-06-2012, 07:00 AM
  3. Try/catch block
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 02:32 PM
  4. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:56 PM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •