0

I created a custom exception so that when I browse for a file and close the browser, I catch the error and tell the user "Please Select a file":

public class CancelException extends Exception {

    public CancelException() {
    }

    public CancelException(String message) {
        super(message);
    }

}

Now I am trying to input it in the load method but I can't see where I can input it as there are already multiple exceptions that were auto-generated by the netbeans:

public static Person loadcons() throws IOException
{
    Person loadcons = null;
    JFileChooser chooser = new JFileChooser();
    int chooserOption = chooser.showSaveDialog(null);
    chooserOption = JFileChooser.APPROVE_OPTION;

    try {
        File file = new File (chooser.getSelectedFile().getAbsolutePath());
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
        loadcons = (Person) input.readObject();
        input.close();
        return loadcons;
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    } catch (ClassNotFoundException ex) {
        System.out.println(ex.getMessage());
    }
    return null;

}

private String toString(String PersonID) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

Any help with this would be really appreciated as I am a beginner in this language.

I tried Creating the exception in the load method:

  public static Person loadcons() throws IOException, CancelException {
    Person loadcons = null;
    try {
        JFileChooser chooser = new JFileChooser();
        int chooserOption = chooser.showSaveDialog(null);
       if (chooserOption == JFileChooser.APPROVE_OPTION)
       {

        File file = new File(chooser.getSelectedFile().getAbsolutePath());
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));

        loadcons = (Person) input.readObject();
        input.close();
        return loadcons;
       }
       else{
           throw new CancelException("ERROR MESSAGE");
       }
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    } catch (ClassNotFoundException ex) {
        System.out.println(ex.getMessage());
    }
    return null;

}

private String toString(String PersonID) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

But the exception is not being thrown

Ryan Theuma
  • 33
  • 10

1 Answers1

0

I would like to suggest below for using your custom exception--

import *.CancelException;

public static Person loadcons() throws IOException,CancelException 
  ..
  ..
try {
    ..
    ..
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    } catch (ClassNotFoundException ex) {
    System.out.println(ex.getMessage());
    }catch (Exception ex) {
        throw new CancelException("Please select a file");
   }
    return null;
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36