Rethrowing exceptions in Java without losing the stack trace

后端 未结 9 2124
挽巷
挽巷 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条回答
  •  猫巷女王i
    2020-11-28 19:59

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

提交回复
热议问题