PDA

View Full Version : IOExceptions




Omega Duck
Feb 6, 2009, 02:20 AM
hi,
I am pretty new to Java and i have been trying to launch an application from the code, as in the user presses a JButton or something and the application opens.

But now I have a problem, i have an "unreported exception java.io.IOException;"

Now I am not sure what to do, I tried to catch it, but i dont know how, and and then i tried to throw it and that did work either.

if (event.getSource() == nItem) {

String command="/usr/bin/open/Applications/Firefox.app";
Process child = Runtime.getRuntime().exec(command);
}

This is the code, I dont know what to do, as in how and where to throw or to catch.

Thanks a lot



foidulus
Feb 6, 2009, 07:17 AM
hi,
I am pretty new to Java and i have been trying to launch an application from the code, as in the user presses a JButton or something and the application opens.

But now I have a problem, i have an "unreported exception java.io.IOException;"

Now I am not sure what to do, I tried to catch it, but i dont know how, and and then i tried to throw it and that did work either.

if (event.getSource() == nItem) {

String command="/usr/bin/open/Applications/Firefox.app";
Process child = Runtime.getRuntime().exec(command);
}

This is the code, I dont know what to do, as in how and where to throw or to catch.

Thanks a lot

First, you need a space between open and /Applications

Second, you can get more information by running the console app and looking at the trace. /Applications/Utilities/Console.app

Finally, and most importantly, you should put the whole thing in a try/catch block regardless of whether or not your code is correct because bad things can and do happen. I won't go into the nuances of exception handling here, but add the following code to your app and run it from the command line or look at the console again

(after the if)
try {
String command="/usr/bin/open/Applications/Firefox.app";
Process child = Runtime.getRuntime().exec(command);
}
catch(Exception e) {e.printStackTrace();}

} //If statement's bracket.

That will tell you what line problem is happening on as well as give you some other useful trace. While catching Exception is a but brute force, for now it will at least get you acclimated to basic exception handling.

Krevnik
Feb 6, 2009, 10:49 AM
hi,
IBut now I have a problem, i have an "unreported exception java.io.IOException;"

Now I am not sure what to do, I tried to catch it, but i dont know how, and and then i tried to throw it and that did work either.

Are you sure you didn't read that as "uncaught exception"? Granted my Java is rusty, but that seems like a really worthless error message if it actually told you 'unreported'.

But what is happening (in this case) is that 'unreported exception' means that someone threw an exception, and the Java runtime had to catch it because you didn't.

Follow the steps the other poster gave you, as they are spot on in this case for someone who is still learning.