Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Better way of using e.printStackTrace()?

  1. #1
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Question Better way of using e.printStackTrace()?

    I've read somewhere that this could be improved:

    try {
        InputStream input = new FileInputStream("myFile.txt");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    to:

    try {
        InputStream input = new FileInputStream("myFile.txt");
    } catch (FileNotFoundException e) {
        e.printStackTrace(); // esto es opcional, obviamente
        throw new RuntimeException(e);
    }

    This way, we relaunching the exception as a RuntimeException our method doesn't have to declare any throws FileNotFoundException (at least for this example) and if the error does happen we'll see it immediately.

    What are your comments on it?

  2. #2
    Moderators helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Better way of using e.printStackTrace()?

    No! This is a very bad idea to do. If you want to propagate the exception up the stack, re-throw the original exception.

    This will maintain the original stack trace, and doesn't add any extra false information which would throw someone debugging the problem off the real problem.

    Here's the output if you wrap and throw a RuntimeException:

    public class Test
    {
    	public static void fail_open(String path, boolean is_url) throws FileNotFoundException, MalformedURLException
    	{
    		try
    		{
    			if(is_url)
    			{
    				throw new MalformedURLException(path);
    			}
    			else
    			{
    				throw new FileNotFoundException(path);
    			}
    		}
    		catch(FileNotFoundException e)
    		{
    			throw e;
    		}
    		catch(MalformedURLException e)
    		{
    			throw e;
    		}
    	}
     
    	public static void main(String[] args) throws Exception // bad practice to throw in main, doubly bad because it's the generic Exception!
    	{
    		fail_open("blah.txt", false);
    	}
    }

    The "proper" default behavior is:
     

    Exception in thread "main" java.io.FileNotFoundException: blah.txt
    at Test.fail_open(Test.java:23)
    at Test.main(Test.java:40)




    And the appropriate message if we wrapped and re-threw a RuntimeException:
     
    Exception in thread "main" java.lang.RuntimeException: java.io.FileNotFoundException: blah.txt
    at Test.fail_open(Test.java:28)
    at Test.main(Test.java:40)
    Caused by: java.io.FileNotFoundException: blah.txt
    at Test.fail_open(Test.java:23)
    ... 1 more




    Notice that all of the line numbers and stack trace of the RuntimeException point to where I re-threw the exception. The print stack trace method for RuntimeExceptions is somewhat smart that it will print a partial stack trace for the wrapped Throwable object, but you don't get the full trace displayed by default.

    There's also have no way to catch different type of exceptions and handle them separately if we wrap it into a generic RuntimeException.

    	public static void main(String[] args) throws Exception
    	{
    		try
    		{
    			fail_open("blah.txt", false);
    		}
    		catch(FileNotFoundException e)
    		{
    			System.out.println("bad file path");
    		}
    		catch(MalformedURLException e)
    		{
    			System.out.println("bad url");
    		}
    		try
    		{
    			fail_open("google.com", true);
    		}
    		catch(FileNotFoundException e)
    		{
    			System.out.println("bad file path");
    		}
    		catch(MalformedURLException e)
    		{
    			System.out.println("bad url");
    		}
    	}

    This just won't work if you wrap your exceptions in a RuntimeException.

    Personally I'm not a big fan of the way checked/unchecked exceptions work in Java, but it's a limitation we have to deal with. Trying to get around this by wrapping everything in a RuntimeException is just using a bad idea to try and "fix" a questionable design.

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    Andrew R (August 18th, 2013), jps (August 17th, 2013)

  4. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Better way of using e.printStackTrace()?

    Hello.
    If we want to convert a checked exception into an unchecked exception (the way various frameworks do) it may be of help.

    Syed.

  5. #4
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Better way of using e.printStackTrace()?

    thank you guys for answering. I just read that, so I had to ask.