Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are exceptions caught and dealt with at the low (assembly) level?

I have this code -

try {
     doSomething();
} catch (Exception e) {
   e.printStackTrace();
}

How will this actually be implemented by the compiler. Where is the check for the exception actually put in the assembly code generated?

Update
I know that how the above code is translated to bytecode. The bytecode only translates the try-catch to corresponding try-handler blocks. I am interested in how it will be translated to assembly/and or handled by the jvm.

like image 554
pdeva Avatar asked Jul 29 '14 02:07

pdeva


People also ask

How are exceptions caught?

You can catch different exceptions in different catch blocks. When an exception occurs in try block, the corresponding catch block that handles that particular exception executes. For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception executes.

Where should exceptions be caught?

You should catch the exception when you are in the method that knows what to do. For example, forget about how it actually works for the moment, let's say you are writing a library for opening and reading files. Here, the programmer knows what to do, so they catch the exception and handle it.

How do you handle exceptions without catching and catching?

throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.

Why are there two catch blocks for handling exceptions in Java?

Hence there are two catch blocks for handling both exceptions. 2. The order of catch blocks does matter If the protected code can throw different exceptions which are not in the same inheritance tree, i.e. they don’t have parent-child relationship, the catch blocks can be sorted any order.

What happens if you catch too many types of exceptions?

Catching an overly broad exception essentially defeats the purpose of .NET's typed exceptions, and can become particularly dangerous if the program grows and begins to throw new types of exceptions. The new exception types will not receive any attention. Example: The following code excerpt handles three types of exceptions in an identical fashion.

Where can I find the AArch64 exception level (El) code?

As always the code is available on GitHub. AArch64 Exception Levels (a.k.a. EL) ARM specification defines four priviledge levels (with a caveat) that are called Exception Levels and numbered from 0 to 3, where EL0is the lowest privledge level and EL3is the highest privledge level.

Why do we handle general exceptions first in Java?

It’s because if we handle the most general exceptions first, the more specific exceptions will be omitted, which is not good, as Java encourages handling exceptions as much specific as possible. 3. Catching one exception for all


1 Answers

If I understand your question correctly, the following code

public class Example {
    public static void main(String[] args) {
        try {
            otherMethod();
        }
        catch (Exception e) {}
        try {
            otherMethod();
            someMethod();
        }
        catch (SQLException e) {}
        catch (IOException e) {}
    }

    public static void someMethod() throws IOException {throw new IOException();}
    public static void otherMethod() throws SQLException, IOException {}
}

produces the following (excerpt of human readable version of) byte code.

//  main method
     0: invokestatic  #2                  // Method otherMethod:()V
     3: goto          7
     6: astore_1      
     7: invokestatic  #2                  // Method otherMethod:()V
    10: invokestatic  #4                  // Method someMethod:()V
    13: goto          21
    16: astore_1      
    17: goto          21
    20: astore_1      
    21: return        
  Exception table:
     from    to  target type
         0     3     6   Class java/lang/Exception
         7    13    16   Class java/sql/SQLException
         7    13    20   Class java/io/IOException

You'll notice the Exception table. This constructs instructs the VM that if an exception of type type happens between the instruction from from to to, then it must goto instruction (offset) target. It also instructs it to push the Exception reference on the stack so that its value can be copied and bound to the parameter in the catch block.

You also have this piece relating to the throw statement above.

// someMethod method
     0: new           #6                  // class java/io/IOException
     3: dup           
     4: invokespecial #7                  // Method java/io/IOException."<init>":()V
     7: athrow        

The instruction athrow does the following

throws an error or exception (notice that the rest of the stack is cleared, leaving only a reference to the Throwable)

The JVM explains what happens

The objectref must be of type reference and must refer to an object that is an instance of class Throwable or of a subclass of Throwable. It is popped from the operand stack. The objectref is then thrown by searching the current method (§2.6) for the first exception handler that matches the class of objectref, as given by the algorithm in §2.10.

If an exception handler that matches objectref is found, it contains the location of the code intended to handle this exception. The pc register is reset to that location, the operand stack of the current frame is cleared, objectref is pushed back onto the operand stack, and execution continues.

If no matching exception handler is found in the current frame, that frame is popped. If the current frame represents an invocation of a synchronized method, the monitor entered or reentered on invocation of the method is exited as if by execution of a monitorexit instruction (§monitorexit). Finally, the frame of its invoker is reinstated, if such a frame exists, and the objectref is rethrown. If no such frame exists, the current thread exits.

So stack frames keep getting popped until one is found that can handle the thrown exception.

How will this actually be implemented by the compiler. Where is the check for the exception actually put in the assembly code generated?

The compiler generates the bytecode above. There is no check for an exception, only byte code instructions. The athrow will instruct the VM to perform the task of what we call throwing an exception, which will result in popping the stack, searching exception tables in the current stack frame, etc.

like image 140
Sotirios Delimanolis Avatar answered Sep 21 '22 15:09

Sotirios Delimanolis