"throw it away" leads to a strange line in the opposite direction

OK, this is one of the worst programming examples ever, but I tried it while studying someone else's question and found the results a bit strange. Any explanation?

public class Test {

    static class Bizarre extends RuntimeException {

        public void throwMe() {
            throw this;                   // line 6
        }

    }

    public static void main(String[] args) {
        Bizarre biz = new Bizarre();             // line 12
        System.out.println("Output line 1");     // line 13
        biz.throwMe();                           // line 14
        System.out.println("Output line 2");     // line 15
    }
}

Result:

Output line 1
Exception in thread "main" Test$Bizarre
        at Test.main(Test.java:12)

Why line 12?

+4
source share
1 answer

Initialization creates a stack trace Exception( Throwable). Your exception, an instance Bizarre, is created on line 12.

+9
source

Source: https://habr.com/ru/post/1544813/


All Articles