2

I have a method to load a customer file by choosing it from the File Open Dialog Box and it works, except for when I click the Cancel button. It still loads the selected file even if I press the Cancel button. I want to load a Custom Exception if I click the Cancel Button. Any help on how to implement the Custom Exception in my method please? Thanks

 private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt) {
   Customer customerfile = null;
   try {

     final JFileChooser chooser = new JFileChooser("Customers/");
     int chooserOption = chooser.showOpenDialog(null);
     chooserOption = JFileChooser.APPROVE_OPTION;

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

     customerfile = (Customer) in .readObject();

     custnameTF.setText(customerfile.getPersonName());
     custsurnameTF.setText(customerfile.getPersonSurname());
     custidTF.setText(customerfile.getPersonID());
     custpassidTF.setText(customerfile.getPassaportID());
     customertellTF.setText(customerfile.getPersonTel());
     customermobTF.setText(customerfile.getPersonMob());
     consnameTF.setText(customerfile.getConsultantname());
     conssurnameTF.setText(customerfile.getConsultantsurname());
     considTF.setText(customerfile.getConsulid());

     in .close();

   } catch (IOException ex) {
     System.out.println("Error Loading File" + ex.getMessage());
   } catch (ClassNotFoundException ex) {
     System.out.println("Error Loading Class");
   } finally {
     System.out.println("Customer Loaded");
   }

 }
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
Luke_i
  • 163
  • 1
  • 2
  • 13

4 Answers4

3

It looks like you're doing an assignment instead of a test on the result of the chooser.

Instead of

chooserOption = JFileChooser.APPROVE_OPTION;

you should have

if (chooserOption == JFileChooser.APPROVE_OPTION) {
    // handle open file
} else {
    throw new CancelException();
}

EDIT

In response to the comment, exceptions should extend either Exception (for checked exceptions), RuntimeException (for unchecked exceptions) or a descendent of one of those classes. The only difference at this level is that you don't need to declare unchecked exceptions in the throws of the method signature. Your exception will look something like this

public class CancelException extends Exception {

    public CancelException() {
    }

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

One other comment - exceptions should be used for exceptional circumstances. It's generally considered bad practice to use them to implement logic - do you actually need to use an exception?

Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
  • Hi, thanks for your answer, now I understand the concept but when I try to insert my Custom Exception its saying "constructor CancelExcpetion in class CancelExcpetion cannot be applied to given types; required String" – Luke_i May 05 '15 at 08:37
2

Make your method declaration to trow your Exception:

private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt)
    throws CustomException {

You are giving always APPROVE_OPTION to chooserOption:

 chooserOption = JFileChooser.APPROVE_OPTION; 

You must make dialog button listener to modify this variable and add a condition:

if (chooserOption == JFileChooser.APPROVE_OPTION) {
    // load file
} else {
    throw new CustomException("ERROR MESSAGE");
}

And your CustomException Class must look like:

class CustomExceptionextends Exception {
    public CustomException(String msg) {
        super(msg);
    }
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Hi, thanks for your answer, now I understand the concept but when I try to insert my Custom Exception its saying "constructor CancelExcpetion in class CancelExcpetion cannot be applied to given types; required String" – Luke_i May 05 '15 at 08:38
  • 1
    totally true, please check my update. you must throw an eception that matches with given constructor: `throw new CustomException("ERROR MESSAGE");` !!!! – Jordi Castilla May 05 '15 at 08:43
  • 1
    Amazing Jordi! Thanks a lot :) – Luke_i May 05 '15 at 08:47
1

You should not assign anyting to chooserOption. You should use a return value of JFileChooser.showOpenDialog(), it holds info about the result of dialog showing. Example:

int chooserOption = chooser.showOpenDialog(null);
if (chooserOption == JFileChooser.CANCEL_OPTION) {
   // throw your exception (or do some other actions) here
}
Luo
  • 445
  • 2
  • 10
0

you are not using the chooserOption, once it has selected value, just add a if condition that check for chooserOption value, if selected, executed else throw your exception

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57