JavaRush /Java Blog /Random EN /Java stack trace
IvanDurov
Level 25

Java stack trace

Published in the Random EN group
The Java Virtual Machine (hereinafter JVM) processes the code and runs methods one after another, starting with method main. When it reaches the next method, it says that this method is at the top of the stack. After a method is fully executed, it is removed from the stack and replaced by the next one in the queue. To demonstrate the principle, type this code: Java stack trace - 1
package errorhandling;

public class errorChecking {
    public static void main(String[] args) {
        System.out.println("Метод Main успешно запущен");
        m1();
        System.out.println("Метод Main заканчивает свою работу");
    }

    static void m1() {
        System.out.println("Первый метод передаёт привет!(m1)");
        m2();
    }

    static void m2() {
        System.out.println("Второй метод передаёт привет(m2)");
    }
}
We have three methods: method main, method m1and method m2. When the program starts, the method is located at the top of the stack main. Inside the method main, the method is called m1. When called, he jumps to the top of the stack. The method m1in turn calls the m2. Now the method m2jumps to the top of the stack, temporarily removing m1. Imagine this for a second - mainon top m1and on top m2! Having done its business, m2it ends and control returns back to m1. The method m1, when completed, is also removed from the stack, and control is again given to the method main. Run your program and look at the output window: The Main method runs successfully The first method says hello!(m1) The second method says hello(m2) The Main method exits If something goes wrong in the method m2, the JVM (Java Virtual Machine), you remember, right?) will look for error handlers, for example a block try … catch. If there are no errors in the m1error handler method, then the exception will be passed to the method m1, in the hope that it will be able to handle it. If it does not detect an error handler here, the exception will again move up the stack, this time to the method main. If the method maindoes not catch the exception, you will get a strange error message printed in the output window. As an example, make your method m2look like this:
static void m2() {
    int x = 10;
    int y = 0;
    double z = x / y;
    System.out.println( z );
    System.out.println("Method Two - m2");
}
This method contains a division by zero error. Here is the full version of the program, check with yours:
package errorhandling;

public class errorChecking {
    public static void main(String[] args) {
        System.out.println("Метод Main успешно запущен");
        m1();
        System.out.println("Метод Main заканчивает свою работу");
    }

    static void m1() {
        System.out.println("Первый метод передаёт привет!(m1)");
        m2();
    }

    static void m2() {
        int x = 10;
        int y = 0;
        double z = x / y;
        System.out.println( z );
        System.out.println("Method Two - m2");
    }
}
Run the program and see what the output window gives you: The Main method ran successfully The first method says hello!(m1) Exception in thread "main" java.lang.ArithmeticException: / by zero at errorhandling.errorChecking.m2(<u>errorChecking. java:17</u>) at errorhandling.errorChecking.m1(<u>Solution.java:11</u>) at errorhandling.errorChecking.main(<u>>Solution.java:5</u>) Process finished with exit code 1 You are looking at something called a stack trace. The three lines underlined in blue refer to your methods, and can be found in: Name_пакета.Name_класса.Name_метода The first line from the top is where the error occurred - in the method m2. Java ensured that it was handled in a way ArithmeticExceptionthat caught division by zero errors. There is no error handler in the m2, m1and methods. mainSo the program processed it with the default error handler. Change your method m1to the following:
try {
    System.out.println("Первый метод передаёт привет!(m1)");
    m2( );
}
catch (ArithmeticException err) {
    System.out.println(err.getMessage());
}
Now we have wrapped the method m2in a block try. In part catch, we use the type of exception that was detected in the stack trace - ArithmeticException. Run the code again and you will see the following in the output window: The Main method ran successfully The first method says hello!(m1) / by zero The Main method exits Notice that the error message was printed as: " / by zero ". The method m2did not execute completely, but was stopped when an error occurred. Control was then transferred back m1. This happened due to the fact that the block catchitself recognized the error; the JVM did not resort to the standard error handler, but displayed a message located between the curly braces of the block catch. Please note that the program itself has not been stopped. The control, as usual, went to the method mainfrom which m1it was called. And the last line of the method mainwas able to display " End Main method ". This is very, very important. If you needed the value from m1, for subsequent work somewhere in main. And if the value is not there, then your program may not work at all as you expect. When you see the stack trace in the output window, just know that the first line is where the problem occurred, and the remaining lines (if there are any of course) are where the exception was propagated up the stack, usually ending with the main. Translation from homeandlearn.co.uk We say thank you to: Sergei Sysoev, Treefeed...
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION