IOExceptions

Discussion in 'Mac Programming' started by Omega Duck, Feb 6, 2009.

  1. macrumors newbie

    Joined:
    Jan 30, 2009
    #1
    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
     
  2. macrumors 6502a

    Joined:
    Jan 15, 2007
    #2
    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.
     
  3. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #3
    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.
     

Share This Page