Reputation: 73
In my program I have to constantly access the hard drive thousands of times to view images (no way around it), occasionally my program gets tripped up on a "file not found IO Exception" most likely because of the many modifications I'm making to the images and re saving quickly. How do I continue my program even if this error occurs, because currently it causes my program to stop?
Code:
filepattern=imageLocation+temp+"image.jpg";
File outputfile = new File(filepattern);
BufferedImage img = null;
try {
img = ImageIO.read(outputfile);
} catch (IOException e) {
}
Note: I have fixed the problem by making sure the file exists first. Thanks for all your help!
Upvotes: 0
Views: 1638
Reputation: 4945
Catch the exception and handle it as needed.
try {
// your code
} catch (<Expected exception> e) {
// handle the exception
}
Upvotes: 1
Reputation: 4863
You might try something like the fragment below. Of course, you will want to encapsulate the actual reading in a try / catch / finally block to make sure you close the file
try {
filesTried++;
[you code]
} catch (IOException e) {
fileErrors++;
Upvotes: 0
Reputation: 1179
There are 3 key words try {} executes the block that can cause problems catch (Exception ex) {} executes code to handle specific exceptions. Instead of Exception you can handle specific exception types finally {} executes cleanup code. Even if exception occurs and breaks the execution flow in try block, this code will always execute.
Upvotes: 0
Reputation: 3698
Surround the statement with a try
catch
.
This will stop the code from crashing and in the catch
block you can write code to deal with the failure.
Upvotes: 0