views:

223

answers:

7

Hi,

I have the following code.

try{
    Twitter twitter = new Twitter(user,password);
    twitter.setStatus(txtStatus.getText());

    JOptionPane.showMessageDialog(null, "Success");
    txtStatus.setText("");
    txtStatus.requestFocus();

}catch(Exception e){
    JOptionPane.showMessageDialog(null, "Some Error.\n" +
        " If you see this after Success Message..Ignore");
}

Here even after I get "Success message" Dialog, the "Some Error" dialog is also appearing. what may be the reason? Shouldnt the flow control escape the catch block if there were no run time errors.

Even if i get an exception also, the "Some Error" dialog is appearing twice.Why is this happenning?

+1  A: 

Your code may actually be called twice. Try putting a System.out.println statement at the top of the code or running it under a debugger and check it is actually only being called once.

atomice
+8  A: 

You've left open the obvious possibility that one of the lines of code after the success dialog is displayed is throwing an exception. You're not catching a specific exception and you're not displaying a backtrace, so there's no way of telling. Start your debugging by using the caught exception's printStackTrace method to find out where it's coming from.

hobbs
+1  A: 

Try printing the stack e.printStackTrace() - there might be an exception after the success message (e.g. a NullPointerException with txtStatus?)

msparer
No, use of `txtStatus` can not throw a `NullPointerException`, it’s already used before the first dialog box.
Bombe
right, failed to spot that one :-)
msparer
+2  A: 

Look at the Exception that you're catching and its stack trace, and you may be enlightened.

My guess: txtStatus is null after your first dialog, or it's the requestFocus() method that throws an Exception.

Michael Borgwardt
A: 

Exception can be thrown in one of this two lines:

txtStatus.setText("");
txtStatus.requestFocus();
If it's showMessageDialog() that is throwing the exception than nothing is going to be displayed unless it's and exception that is specific to the case when second parameter is "Success".
quosoo
Sorry, I don't understand your thought... I believe that if lakshmanan see the success message, than the problem is not in this line. It remains only two lines of code that can throw an exception.
A: 

I agree with atomice here: your code is being called multiple times. Add the finally block + more sensible error feedback.

try{
    Twitter twitter = new Twitter(user,password);
    twitter.setStatus(txtStatus.getText());

    JOptionPane.showMessageDialog(null, "Success");
    txtStatus.setText("");
    txtStatus.requestFocus();

}
catch(Exception e){
    JOptionPane.showMessageDialog(null, "Some Error.\n" + e.getMessage());
}
finally {
    JOptionPane.showMessageDialog(null, "Finally");
}
I checked your solution, it shows "Finally" dialog after success message, then again it shows "Some Error" and "Finally" dialog. This is the order i getSuccess DialogFinally DialogSome Error DialogFinally Dialog
lakshmanan
There you go. That means that you are not (as expected and contrary to what you thought) getting both a success and error message in on iteration over the block: It is getting called multiple times.
A: 

And I'll throw my two cents in as well.

Stick a break point on the first line and watch it with a debugger. You'll quickly see if it is being run twice, if something is null, and where the error is.

The debugger is your friend :-)

Mr Jacques
I checked it in Debugger mode, the above actually is in an actionPerformed() method (event listener on a button click event). the same method is executed twice ....
lakshmanan
I have an actionListener class(responding to a button click event) and there is the actionPerformed() method. The above code is the only code in that method. Why is this getting executed twice ??
lakshmanan