Why is the catch block executed twice for one exception?
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 the "Successful message" dialog, the "Some error" dialog appears. what could be the reason? If the flow control did not exit the catch block, if there were no runtime errors.
Even if I get the exception, the "Some error" dialog also appears. Why is this happening?
source to share
You left open the obvious possibility that one of the lines of code after the success dialog displays an exception. You won't catch any particular exception, and you don't show a line back, so you can't tell. Start your debugging with the excluded method printStackTrace
that was discovered to find out where it came from.
source to share
I agree with atom here: your code gets called multiple times. Add finally block + more sensible 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");
}
source to share