Home > database >  try catch and throw - still get "unreported exception"
try catch and throw - still get "unreported exception"

Time:05-11

About the Java code below, NetBeans complains about the throw statement, stating there is an "unreported exception IOException", even though I am already catching it...

public class MyClass {
    
    public static java.util.Properties properties = new java.util.Properties();
    static {
        try {
            properties.load(new java.io.FileInputStream("my.properties"));
        }
        catch (java.io.IOException somethingbad) {
            throw somethingbad;
        }
    }
}

If I replace throw somethingbad; with ; there is no complaining. I want to understand why this is and how to properly handle it. I want the program simply to stop if there is such exception, hence why I re-throw it.

I checked the answers to this question unreported IOException even though in try-catch block and throws Exception but they did not enlighten me.

This class is standing alone for now. No other class is referencing it.

CodePudding user response:

What you are doing is rethrowing same exception back to caller of your static block.

As per Java specs, it is compile time error if static block throws checked exception class.

In your case, since you are using FileInputStream which eventually needs to handle with either FileNotFoundException or IOException & that is reason when you rethrow checked IOException it gives you compile time error. If you really want to stop your execution after this exception occur, then simply you can print stacktrace within your code block:

class MyClass {
    
    public static java.util.Properties properties = new java.util.Properties();
    static {
        try {
            properties.load(new java.io.FileInputStream("my.properties"));
        }
        catch (IOException e) {
           e.printStackTrace(); //just to give info why exception occurred.
        }
    }
}

CodePudding user response:

In Java there are two types of Exceptions :

1- Checked Exceptions

2- Unchecked Exceptions

Handle : using a try catch block : the line that may cause the exception should be in the try block , the catch block run when the exception trown .

Declare : when declaring a method that can cause an Exception , you can add the syntax : throws Exception_Class_Name to inform the caller that this method may caontain a code that can probably throw an Exception of type Exception_Class_Name.


Unchecked Exceptions dont cause a problem because they are not required to be handled or declared , but there is not problem if you handle or declare.

Checked Exceptions are required to be Handled Or declared (you can handle and declare in the same time).

Looking at your example you have an exception of type IOException named "somethingbad" , in java code any line that throw surely a Checked Exception must be handled or declared , so the line :

    throw somethingbad;

must be handled or declared , here your line is inside a static block , we can declare Exception when talking about a code inside a methods, so here you are required to handle the Exception using a try catch block , putting your line inside the try and writing a catch block that can catch the exact type or a supertype of this check Exception, if you need that your program stop excecution you can simply throw an Unchecked Exception ( RuntimeException for example).

Handling the IOEXception :

import java.io.IOException;
public class MyClass {

    public static java.util.Properties properties = new java.util.Properties();
    static {
        try {
            properties.load(new java.io.FileInputStream("my.properties"));
        } catch (IOException somethingbad) {
            try {
                throw somethingbad;
            } catch (IOException e) {
                e.printStackTrace();    

            }
        }
    }
}

Throwing an Unchecked Exception :

import java.io.IOException;
public class MyClass {

    public static java.util.Properties properties = new java.util.Properties();
    static {
        try {
            properties.load(new java.io.FileInputStream("my.properties"));
        } catch (IOException somethingbad) {
            throw new RuntimeException(somethingbad.getMessage());
        }
    }
}
  • Related