Jvm exception

So, I recently wrote a java class that extends an exception and used an instance of that class to check for cases and throw myself if an error occurs. I found out that when the caller catches this exception, the line that says the exception was thrown is the line from which the exception was thrown, not where it was thrown from. I'm just wondering why this is the case, and if this is the intended behavior for jvm or not, since this is not a common way to throw exceptions. If this is the intended behavior, then what is rational for it, since it seems that the line number from which the exception was thrown would be more useful (and probably easier to track through the stack). Sample examples demonstrate expected behavior and unexpectedness.


Plain throwing exceptions:

1  public class Test
2  {
3   public static void main(String ... args) throws Throwable
4   {
5       switch(5)
6       {
7           case 1: throw new Exception("Exception");
8           case 2: throw new Exception("Exception");
9           case 3: throw new Exception("Exception");
10          case 4: throw new Exception("Exception");
11          case 5: throw new Exception("Exception");
12      }
13  }
14 }

      

output:

Exception in thread "main" java.lang.Exception: Exception
    at Test.main(Test.java:11)

      


My approach (simplified):

1  public class Test
2  {
3   public static void main(String ... args) throws Throwable
4   {
5       Exception e = new Exception("Exception");
6       switch(5)
7       {
8           case 1: throw e;
9           case 2: throw e;
10          case 3: throw e;
11          case 4: throw e;
12          case 5: throw e;
13      }
14  }
15 }

      

output:

Exception in thread "main" java.lang.Exception: Exception
    at Test.main(Test.java:5)

      

+3


source to share


2 answers


the line that says the exception was thrown is the line from which the exception was thrown, not where it was thrown. I'm just wondering why this is the case and if this is the intended behavior for jvm or not



Yes. It is not only intended but also documented. See the Javadoc for java.lang.Throwable

: "A throw contains a snapshot of its thread's execution stack at the time of its creation." If you don't want this, you have the option to call fillInStackTrace()

before throwing it.

+7


source


From JavaDoc for Throwable :

Typically, this is the point at which this throwable was created and thrown.

This is a bit ambiguous, but 99% of the time, an exception is thrown where it is thrown. My guess is that creating a stack trace from the point of creation makes more sense if you're considering re-throwing exceptions, for example:

void bar() throws Exception
{
    throw Exception();
}

void foo() throws Exception
{
    try
    {
        bar();
    }
    catch (Exception e)
    {
        throw e;
    }
}

public static void main(String[] args)
{
   foo();
}

      



It is more useful to know that an exception was thrown in bar()

than knowing that it was re-thrown in foo()

. If this information is important, you can throw a new exception with the original as the reason argument.

UPDATE

To illustrate, if you really wanted to know that the exception was thrown in foo()

, then you would do it infoo()

void foo() throws Exception
{
    try
    {
        bar();
    }
    catch (Exception e)
    {
        throw new Exception("Some more useful information from foo", e);
    }
}

      

+1


source







All Articles