Rethrowing exceptions in Java without losing the stack trace

JavaException

Java Problem Overview


In C#, I can use the throw; statement to rethrow an exception while preserving the stack trace:

try
{
   ...
}
catch (Exception e)
{
   if (e is FooException)
     throw;
}

Is there something like this in Java (that doesn't lose the original stack trace)?

Java Solutions


Solution 1 - Java

catch (WhateverException e) {
    throw e;
}

will simply rethrow the exception you've caught (obviously the surrounding method has to permit this via its signature etc.). The exception will maintain the original stack trace.

Solution 2 - Java

You can also wrap the exception in another one AND keep the original stack trace by passing in the Exception as a Throwable as the cause parameter:

try
{
   ...
}
catch (Exception e)
{
     throw new YourOwnException(e);
}

Solution 3 - Java

I would prefer:

try
{
    ...
}
catch (FooException fe){
   throw fe;
}
catch (Exception e)
{
    // Note: don't catch all exceptions like this unless you know what you
    // are doing.
    ...
}

Solution 4 - Java

In Java is almost the same:

try
{
   ...
}
catch (Exception e)
{
   if (e instanceof FooException)
     throw e;
}

Solution 5 - Java

In Java, you just throw the exception you caught, so throw e rather than just throw. Java maintains the stack trace.

Solution 6 - Java

something like this

try 
{
  ...
}
catch (FooException e) 
{
  throw e;
}
catch (Exception e)
{
  ...
}

Solution 7 - Java

Stack trace is prserved if you wrap the catched excetion into an other exception (to provide more info) or if you just rethrow the catched excetion.

try{ ... }catch (FooException e){ throw new BarException("Some usefull info", e); }

Solution 8 - Java

public int read(byte[] a) throws IOException {
	try {
		return in.read(a);
	} catch (final Throwable t) {
		/* can do something here, like  in=null;  */
		throw t;
	}
}

This is a concrete example where the method throws an IOException. The final means t can only hold an exception thrown from the try block. Additional reading material can be found here and here.

Solution 9 - Java

I was just having a similar situation in which my code potentially throws a number of different exceptions that I just wanted to rethrow. The solution described above was not working for me, because Eclipse told me that throw e; leads to an unhandeled exception, so I just did this:

try
{
...
} catch (NoSuchMethodException | SecurityException | IllegalAccessException e) {					
	throw new RuntimeException(e.getClass().getName() + ": " + e.getMessage() + "\n" + e.getStackTrace().toString());
}

Worked for me....:)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionripper234View Question on Stackoverflow
Solution 1 - JavaBrian AgnewView Answer on Stackoverflow
Solution 2 - JavaOlvagorView Answer on Stackoverflow
Solution 3 - JavaMarkus LausbergView Answer on Stackoverflow
Solution 4 - JavaalvesView Answer on Stackoverflow
Solution 5 - JavaDavid MView Answer on Stackoverflow
Solution 6 - JavaGwegView Answer on Stackoverflow
Solution 7 - JavaSindreView Answer on Stackoverflow
Solution 8 - JavaDanielView Answer on Stackoverflow
Solution 9 - JavaMatthiasView Answer on Stackoverflow