Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

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)?

1 Answer

0 votes
by (46k points)

You can try:

catch (WhateverException e) {

    throw e;

}

It will just rethrow the exception you've passed (certainly the enclosing method has to allow this via its signature etc.). The exception will preserve the initial stack trace.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 9, 2019 in Java by Nigam (4k points)

Browse Categories

...