Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with throws IOException problem

    Hi guys

    I created a method named SaveEntry that writes the data in my object array into a file but I'm having dificulties with throws IOException line. I entered the line at the top which is in red right after i declared the method but when I compile it, it points to the line where I call SaveEntry and gives me this error: "unreported exception java.io.IOException; must be caught or declared to be thrown"

    Code:
    else if (input.equals("SAVE ENTERED DATA"))
    
    SaveEntry(vehicleArray,n);
    
    //above located in another method
    
    public static void SaveEntry(Vehicle [] vehicleArray,int maxInput) throws IOException
    {
        String fileInput = JOptionPane.showInputDialog("enter the desired name of the save file");
        String fileName = fileInput + ".txt";
        FileWriter outputFile = new FileWriter(fileName);
        BufferedWriter outputBuffer = new BufferedWriter(outputFile);
        PrintWriter printstream = new PrintWriter(outputBuffer);
    
    
    
        for(int c=0;c<maxInput;c++ )
        {                                 
          printstream.println(vehicleArray[c]);
                                                        
        }//end for
        printstream.close();
    
    }//end SaveEntry method

    Is it asking me to put the throws IOException line where I call SaveEntry? If yes where do i put it? I tried putting it after the brackets but I get errors saying its not a statement and I'm missing a semicolon.

    Any help will be greatly appreciated.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You have created a checked exception.
    Your saveEntry call must be wrapped in a try/catch block, or re-throw from the method calling the saveEntry method (somewhere you need to deal with it though).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Here's what Fou-Lu meant to say.

    - Either you have to catch IOException or rethrow it

    Code:
    try {
    SaveEntry(vehicleArray,n);
    
    } catch(IOException e) {
    //do something here
    }

  • Users who have thanked servlet for this post:

    budoy (03-03-2011)

  • #4
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I amended my code and it works. Thank you very much.One last question where you put do something here, does it have to be anything particular? Can i put anything I want like a an error messsage?

  • #5
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Yes, you can handle the exception the way you want.. like log a message, rethrow it, or display an error message to user etc.

  • #6
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah I see. Thanks again for the clarification.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •