Ok i have a mthod that writes data to a file and throws an exception

import java.io.*; 
public void writeBook()throws IOException
    {
    	String filename = JOptionPane.showInputDialog("Please enter a file name to add the book details in.");
    	FileWriter outputFile = new FileWriter(filename);
   	    BufferedWriter outputBuffer = new BufferedWriter(outputFile);
        PrintWriter printStream = new PrintWriter(outputBuffer); 
        	
        for(int i=1;i<bookIndex;i++)
        {        	
        printStream.println(books[i].getTitle());
        printStream.println(books[i].getNoOfCopies());
        printStream.println(books[i].getBookNo());
        printStream.println(books[i].getAuthor());
        }
        printStream.close();	
        
    }

The problem is that if i try to call this method ( like this: writeBook(); ) from somewhere else in the class, it gives me an error:

unreported exception java.io.IOException; must be caught or declared to be thrown

HELP???

Edited 6 Years Ago by smokin745: n/a

That is because that thrown exception must be handled at some point. So either you encapsulate your writeBook(); method in try-catch there and deal with exception or you pass it up. Application main method is your roof in this scenario.

can you give me some more details or code, i can't understand how do i do try and catch or pass it up. Please tell me how to call this method.

Sorry at above, but I cannot understand and solve my problem with that link.

Yes you can, you just don't want to take the time to. Rather than a cut-n-paste answer (which you'll understand less than nothing of) make sure you know the concept, as well as, the construct. Read that tutorial!

Edited 6 Years Ago by masijade: n/a

This article has been dead for over six months. Start a new discussion instead.