Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA + try catch(FileNotFoundException e) going in catch(Exception e)?

I have some command which creates a file on disk. Because the folder in which the file has to be created is dynamic, I have a catch(FileNotFoundException e). In the same try block, I already have a catch(Exception e) block. For some reason, when I run my code and the folder does not exists yet, the catch(Exception e) block is used, not the FileNotFoundException one.

The debugger is clear though (to me at least), showing a FileNotFoundException: java.io.FileNotFoundException: c:\mydata\2F8890C2-13B9-4D65-987D-5F447FF0DDA7\filename.png (The system cannot find the path specified)

Any idea why it doesn't go into the FileNotFoundException block? Thanks;

CODE:

import java.io.FileNotFoundException;

try{
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle screenRectangle = new Rectangle(screenSize);
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(screenRectangle);
    ImageIO.write(image, "png", new File(fileName));
}
catch (FileNotFoundException e){
    // do stuff here..
    return false;
}
catch(Exception e){
    // do stuff here..
    return = false;
}
like image 862
user706058 Avatar asked May 05 '11 13:05

user706058


People also ask

What is catch exception E in java?

Java try and catch The 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.

How do I fix java IO FileNotFoundException?

How to Fix FileNotFoundException. Since FileNotFoundException is a checked exception, a try-catch block should be used to handle it. The try block should contain the lines of code that can throw the exception and the catch block should catch and handle the exception appropriately.

What is an Catch exception?

Each catch block is an exception handler that handles the type of exception indicated by its argument. The argument type, ExceptionType , declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class. The handler can refer to the exception with name .

What is FileNotFoundException in java?

Class FileNotFoundExceptionSignals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname does not exist.


2 Answers

It's also possible that the specific issue you're having isn't a FileNotFoundException. By using the "Exception" in a catch block (which is the parent class to all Exceptions) this is effectively a "catch all", since it will run if there is an `Exception or any of its subclasses thrown.

Try the following change:

...

catch (Exception e) {
  System.out.println(e.getClass());
}
...

This will tell you the specific class of the Exception being caught by this block. I'll bet you'll find that the Exception is actually an instance of a subclass (such as IOException, for example).

like image 77
jefflunt Avatar answered Oct 21 '22 09:10

jefflunt


Your problem is that the FileNotFoundException is thrown somewhere deep inside the java library and not propagated up so you cannot catch it. The real culprit here is a NullPointerException originating from the

ImageIO.write(image, "png", new File(fileName));

call. This one runs into your catch (Exception e) block.
If you add a catch (NullPointerException e) block before your general Exception catch, you will see it going in there.

like image 36
Marco Avatar answered Oct 21 '22 07:10

Marco