I use Eclipse to write the code and I get a red underline at customHandler.saveTransactionToFile(); and it says
Unhandled exeption type IOException.
Why does this happen and how could I solve it?
// Call method in customHandler class to write to file when button is pressed
public void actionPerformed(ActionEvent event)
{
// Save transactions to file
if(event.getSource()== buttonSaveTransaction)
{
customHandler.saveTransactionToFile();
}
}
// Method in class customHandler that writes to file
public void saveTransactionToFile() throws IOException
{
System.out.println("Skriver till fil");
File outFile = new File("C:/JavaBank/" + selectedCustomerAccountNumber + ".data");
FileOutputStream outFileStream = new FileOutputStream(outFile);
PrintWriter outStream = new PrintWriter(outFileStream);
outStream.println("test");
outStream.close();
}
Inside your actionPerformed() method ,, write
write this like
To answer as to why you have to do this “It’s because the method being called for i.e.
customHandler.saveTransactionToFile();, is known to throw IOException as mentioned by you in the definition.”Hope that will help
Regards