Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

try...throw new ...catch

Status
Not open for further replies.

patricktwo

Programmer
Jul 6, 2002
10
US
I am writtin a proggram for a class, I have to use exceptios. I have written this:

public void checkFirstNameInput()
{
try
{
if(sFirstName.length() == 0)
throw new FirstNameException("No Entry");
inputLastName.requestFocus();
}
catch (FirstNameException e)
{
JOptionPane.showMessageDialog(frame, "You must enter a valid name!", e.getMeesage, JOptionPane.ERROR_MESSAGE);
inputFirstName.requestFocus();
}

I get an error message "cannot resolve symbol" in both places that the exception is named.
 
Hi Patricktwo,

It looks like it can not find FirstNameException. Presumably you have written this somwhere else but is not visible to checkFirstNameInput(). Without seeing how it is written, I can not guess what is wrong.

The following compiles and might be some help:

String sFirstName;
Component inputLastName;
Component inputFirstName;
Frame frame;

public void checkFirstNameInput() {
try {
if (sFirstName.length() == 0)
throw new Exception("No Entry");
inputLastName.requestFocus();
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "You must enter a valid name!", e.getMessage(), JOptionPane.ERROR_MESSAGE);
inputFirstName.requestFocus();
}
}

scrat
 
I found that to have a named exception, you have to compile a separate class that names the named exception.
Your reply got me to thinking about that.
Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top