Hello and thanks in advance for your time.
Trying to learn how to throw App Exceptions. Is there any way to get this exception from properties so I can pass it to a JOptionPane?

Model Resources

 UserException=Subscription failed. \
    Please try another email address.
ProfileException=Must Select a Profile \

UnenrollException=Unsubscription failed. \
    Please contact the Webmaster.
LoginException=Login failed. \
    Please contact the Webmaster.
UnknownUserException=Unknown subscriber. \
    Please subscribe.
IncorrectPasswordException=Incorrect password. \
    Please try to login again.
IncorrectProfileException=Profile incorrect.\
    Please try choosing profile again.

LoginInfoBean.java

 public String loginAction(String userName,char[] password,String profile) {
        this.userName = userName;
        password.toString();
        this.profile=profile;
        try{
        if(userName!=null){
            //nothing yet

        }
        if(password.toString()!=null){
            //nothing yet
        }
        }catch(LoginException e){

        }

ModelException.java

package model.err;

import model.ModelUtils;

public class ModelException extends Exception {
    public ModelException(String messageKey) {
        super(ModelUtils.getResource(messageKey));
    }
}

LoginException.java

package model.err;

import model.ModelUtils;

public class ModelException extends Exception {
    public ModelException(String messageKey) {
        super(ModelUtils.getResource(messageKey));
    }
}

LoginException.java

package model.err;

import model.ModelUtils;

public class ModelException extends Exception {
    public ModelException(String messageKey) {
        super(ModelUtils.getResource(messageKey));
    }
}

Before I tried the netbeans won't comple the program if the login exception is not actually thrown in the body of the code.
How do I fix that one?
Thanks
Steve

Recommended Answers

All 12 Replies

Is there any way to get this exception from properties so I can pass it to a JOptionPane?

You can't pass an Exception to a method. The point of an Exception is to signal that something went wrong and to give the person who called the method the opportunity to deal with it.

You can't pass an Exception to a method. The point of an Exception is to signal that something went wrong and to give the person who called the method the opportunity to deal with it.

this is true but if you notice my exceptions strings are in the .properties file. I would like to learn some way to throw the exception
and have the program go to the .properties sheet and get the string and I will write a class that has the JOptionPane to get strings and show the user the problem.

You can't pass an Exception to a method. The point of an Exception is to signal that something went wrong and to give the person who called the method the opportunity to deal with it.

False yet true.
You CAN pass an Exception to a method. In fact that's exactly what many logging APIs do all the time to properly log them :)
I can fully understand someone wanting to pass an Exception to a GUI control in order to have that control show a proper error message to the user.

A JOptionPane however doesn't know how to do that. You'll instead have to have the method that pops up that JOptionPane analyse the Exception and generate a String with the error message to be shown, then pass that String to the JOptionPane.

If you are the one throwing the Exception, then what to do after that Exception occurs is completely up to the user. You can't force them to print a certain message or to view a certain message unless you do so from within the method that throws the Exception. In other words, if you want to force the user to view an error message through a JOptionPane, you might as well get rid of the Exception altogether. The whole idea of an Exception is to give the user the freedom to deal with whatever condition that just occurred, not to force them to view a certain error message with a pop-up.

False yet true.
You CAN pass an Exception to a method. In fact that's exactly what many logging APIs do all the time to properly log them :)
I can fully understand someone wanting to pass an Exception to a GUI control in order to have that control show a proper error message to the user.

That is interesting. Although I'm assuming you would have to declare the fact that you're going to be passing the Exception in advance (and either way, JOptionPane would have no code to deal with the Exception even if you could otherwise pass it).

A JOptionPane however doesn't know how to do that. You'll instead have to have the method that pops up that JOptionPane analyse the Exception and generate a String with the error message to be shown, then pass that String to the JOptionPane.

Agreed, which voids the point of using an Exception to begin with. In that case, in whatever method that generates the Exception, you may as well get rid of the Exception and just print an error message to a JOptionPane to begin with.

I am somewhat farmiliar wit

}catch(IOExeption e){
printstacktrace();
}

... if the method throws that exception it excepts it

how can I make my method except that it will throw a login exception

if(userName!=null{

}catch(LoginException e){
//do stuff..
}

False yet true.
You CAN pass an Exception to a method. In fact that's exactly what many logging APIs do all the time to properly log them :)
I can fully understand someone wanting to pass an Exception to a GUI control in order to have that control show a proper error message to the user.

A JOptionPane however doesn't know how to do that. You'll instead have to have the method that pops up that JOptionPane analyse the Exception and generate a String with the error message to be shown, then pass that String to the JOptionPane.

Sounds good. I was just wondering if there was a cooler way. I saw it done in a web application but it had Faces or something to do stuff that I thought could be done here. In fact I don't recall them using a JOptionPane to display the message it was added to a <message > tag of in a JSP.

That is interesting. Although I'm assuming you would have to declare the fact that you're going to be passing the Exception in advance (and either way, JOptionPane would have no code to deal with the Exception even if you could otherwise pass it).


Agreed, which voids the point of using an Exception to begin with. In that case, in whatever method that generates the Exception, you may as well get rid of the Exception and just print an error message to a JOptionPane to begin with.

In general, I prefer handling Exceptions to dealing with return codes.
If a method runs to completion, no need to handle the return code and check if there's an error.
If not, the exception is propagated to a level where it can be handled with minimum fuzz :)

I did not see anything that explained what is happening in my code.

it tells me.

exception model.err.LoginException is never thrown in body of corresponding try statement

actually I added this code and it will allow me to continue figuring exceptions out

throw new LoginException();

thanks

That actually fixed the problem. now all the exception classes can be used to send the string from properties to the JOptionPane.
Thanks all for the insight.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.