user1244940
user1244940

Reputation: 9

Catch without try error

I would like to get sum results from two textfields. I wanted a way to return nill in the result textbox. Why is the following code throwing "cath without try" error. Where am I wrong.Please assist.

private void AddButton4ActionPerformed(java.awt.event.ActionEvent evt) {
  try
   {
    int x = Integer.parseInt(FirstNumberTextField.getText());
    int y = Integer.parseInt(SecondNumberTextField.getText());
    ResultTextField1.setText((x + y)+"");
   {
   catch(Exception e)
   {
       ResultTextField1.setText("");
   }       
}

Upvotes: 0

Views: 20579

Answers (4)

David Schwartz
David Schwartz

Reputation: 182763

You have a { instead of a } at the end of the try block. This puts the catch block inside it.

Upvotes: 2

Christian.K
Christian.K

Reputation: 49250

The brace before the catch block must be closing brace }, but you've written another open brace {. In fact all blocks (if, else, try, catch, finally, while, for, etc.) always come in balanced sequences of {}

Upvotes: 0

nfechner
nfechner

Reputation: 17525

You have one opening brace too many:

private void AddButton4ActionPerformed(java.awt.event.ActionEvent evt) {
  try
   {
    int x = Integer.parseInt(FirstNumberTextField.getText());
    int y = Integer.parseInt(SecondNumberTextField.getText());
    ResultTextField1.setText((x + y)+"");
   } // <-- This one was wrong.
   catch(Exception e)
   {
       ResultTextField1.setText("");
   }       
}

Upvotes: 6

stevevls
stevevls

Reputation: 10843

You've got an open brace at the end of your try block instead of a closing brace.

   {
   catch(Exception e)

should be

   }
   catch(Exception e)

Upvotes: 1

Related Questions