Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiled class problem in java try/catch block

Tags:

java

I have try and catch block in JAVA code

import java.io.FileOutputStream;
import java.util.zip.ZipOutputStream;

public class TryTest {

    public static void main(String[] args) {
        String zipPath ="D:/test";
        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipPath))){
            String Hello ="Hello";
            System.out.println("==============>"+Hello);
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

}

And my compiled class look like

/* * Decompiled with CFR 0.145. */ ....

try {
    try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(string));){
        String string2 = "Hello";
        System.out.println("==============>" + string2);
    }

....

I wounder why another try block added in compile time.

Full Source code in

https://github.com/vikram06/java_try_catch_bug

like image 546
Vikram R Avatar asked Aug 01 '19 09:08

Vikram R


People also ask

Can we catch compile time error in Java?

During compilation, the compiler has no technique to detect these kinds of errors. It is the JVM (Java Virtual Machine) that detects it while the program is running. To handle the error during the run time we can put our error code inside the try block and catch the error inside the catch block.

What is try-catch block in Java?

Java try and catchThe try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Can I execute multiple catch blocks without try will it give me compile time error in Java?

You cannot have multiple try blocks with a single catch block. Each try block must be followed by catch or finally. Still if you try to have single catch block for multiple try blocks a compile time error is generated.

Can we use try-catch in class?

It must be used within the method. If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception. Java try block must be followed by either catch or finally block.


2 Answers

This is explained in the JLS 14.20.3.2 Extended try-with-resources :

The meaning of an extended try-with-resources statement:

try ResourceSpecification
    Block
Catchesopt
Finallyopt

is given by the following translation to a basic try-with-resources statement (§14.20.3.1) nested inside a try-catch or try-finally or try-catch-finally statement:

try {
    try ResourceSpecification
        Block
}
Catchesopt
Finallyopt

The effect of the translation is to put the ResourceSpecification "inside" the try statement. This allows a catch clause of an extended try-with-resources statement to catch an exception due to the automatic initialization or closing of any resource.

Furthermore, all resources will have been closed (or attempted to be closed) by the time the finally block is executed, in keeping with the intent of the finally keyword.

like image 134
Arnaud Avatar answered Oct 14 '22 11:10

Arnaud


When you're using try with resources (I mean try (...) {... ) then Java compiler generates additional code section to display the stacktrace from local variable of type Throwable. That's because Java compiler is decomposing try with resources statement into separate tries - one for closing the resource and another for statements inside your try.

How is it displayed after decompilation - it depends on the decompiler you use.

like image 44
Andrzej Jaromin Avatar answered Oct 14 '22 12:10

Andrzej Jaromin