Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JVM "throw" an exception

I know that JVM has an exception table that maps possible exceptions that can be thrown in given bytecode indexes. I also read that athrow bytecode throws excetion of reference type that is present on the top of the stack. My question relates more to how instructions like irem "throw" exceptions.

Does the JVM check the top of stack after every instruction execution to check if there is an exception? Would appreciate any insight on that is happening under the hood.

like image 925
karmanaut Avatar asked Nov 23 '14 07:11

karmanaut


People also ask

How does the JVM handle exceptions thrown in the main method?

If main throws an exception, the JVM by default catches it, prints the stack trace, and shuts down.

How does throwing an exception work?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.

How do you throw an exception in Java?

Example. Throw an exception if age is below 18 (print "Access denied"). If age is 18 or older, print "Access granted": public class Main { static void checkAge(int age) throws ArithmeticException { if (age < 18) { throw new ArithmeticException("Access denied - You must be at least 18 years old."); } else { System.

How JVM propagate exception in program explain with example?

Exception propagation : An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method. After a method throws an exception, the runtime system attempts to find something to handle it.


1 Answers

irem is the "logical int remainder" operator. The Java Virtual Machine specification writes:

Run-time Exception

If the value of the divisor for an int remainder operator is 0, irem throws an ArithmeticException.

How the JVM implementation accomplishes that is not specified. It can instruct the CPU to compare the divisor with zero before performing the division, or perfom the division, and react to however the CPU in question signals that a division by 0 occured. As divisions by zero are likely rare, the latter strategy is probably more efficient.

For instance, the Intel 64 and IA-32 architectures software developer's manual combined volumes 3A, 3B, and 3C: System programming guide writes:

6.1 INTERRUPT AND EXCEPTION OVERVIEW

Exceptions occur when the processor detects an error condition while executing an instruction, such as division by zero. The processor detects a variety of error conditions including protection violations, page faults, and internal machine faults. The machine-check architecture of the Pentium 4, Intel Xeon, P6 family, and Pentium processors also permits a machine-check exception to be generated when internal hardware errors and bus errors are detected.

When an interrupt is received or an exception is detected, the currently running procedure or task is suspended while the processor executes an interrupt or exception ha ndler. When execution of the handler is complete, the processor resumes execution of the interrupted procedure or task. The resumption of the interrupted procedure or task happens without loss of program continuity, unless recovery from an exception was not possible or an interrupt caused the currently running program to be terminated.

The JVM would therefore define that exception handler to create an exception object, leave a reference to it in a well known register, and then proceed as for an athrow bytecode instruction.

like image 90
meriton Avatar answered Sep 29 '22 23:09

meriton