Rethrowing exceptions in Java without losing the stack trace

后端 未结 9 2123
挽巷
挽巷 2020-11-28 19:18

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

try
{
   ...
}
catch (Exception e)
{
   if (e is         


        
9条回答
  •  生来不讨喜
    2020-11-28 19:58

    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);
    }
    

提交回复
热议问题