Java Unreported Exception

Java unreported exception

What you're referring to are checked exceptions, meaning they must be declared or handled. The standard construct for dealing with files in Java looks something like this:

InputStream in = null;
try {
in = new InputStream(...);
// do stuff
} catch (IOException e) {
// do whatever
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
}
}
}

Is it ugly? Sure. Is it verbose? Sure. Java 7 will make it a little better with ARM blocks but until then you're stuck with the above.

You can also let the caller handle exceptions:

public void doStuff() throws IOException {
InputStream in = new InputStream(...);
// do stuff
in.close();
}

although even then the close() should probably be wrapped in a finally block.

But the above function declaration says that this method can throw an IOException. Since that's a checked exception the caller of this function will need to catch it (or declare it so its caller can deal with it and so on).

Unreported exception java.lang.Exception; must be caught or declared to be thrown

public static byte[] m16h(byte[] m) throws Exception

The signature of your method indicates that an Exception is susceptible of being thrown.

This means that the exception either :

  1. Must be handled by the caller

    try {
    System.out.println(xor(m16h(add(xor(xor(m16h(add(k1, m16h(add(k2, m16h(k3))))), k3), k2), k1)), k3));
    } catch (Exception e) {
    e.printStackTrace();
    }
  2. Must be rethrowed by the caller

    public static void main(String[] args) throws Exception

ERROR: unreported exception java.io.IOException; must be caught or declared to be thrown

You have to use throws IOException like this :

public static void main(String[] args) throws FileNotFoundException, IOException 

Or you can surround your statement with try{}catch(...){} :

try {
//Your code ...
} catch (IOException ex) {
//exception
}

Unreported Exception. Must be caught or declared to be thrown

Unreported Exception Error means that you are calling a method that could possibly throw an exception and needs to be handled. In this case, you would need to either put that method around a try-catch block to catch the exception or throw it so it could be handled by something else.

Apologies, I just noticed you handled the IOException. The other error you are getting is the RuntimeException. This exception is thrown from when you create the XSSFWorkbook object. The parameter you are putting in the constructor is of type File when it requires a type InputStream or OPCPackage. Just create a FileInputStream like so:

InputStream is = new FileInputStream(OGFile);
XSSFWorkbook wb = new XSSFWorkbook(is);

Then you should not have anymore unhandled/thrown errors.

unreported exception despite of try-catch block

You have declared that the method throws an exception - which it doesn't:

public String fileNotFoundExTest() throws FileNotFoundException {

Just change to:

public String fileNotFoundExTest() {

Error: unreported exception IllegalMoveException; must be caught or declared to be thrown

Catch an exception using try-catch block, like this:

//...
try{
removePair();
}catch(IllegalMoveException e){
//...
}
//...

 

//...
try{
removeLowCard();
}catch(IllegalMoveException e){
//...
}
//...


Related Topics



Leave a reply



Submit