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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exceptions

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
CA
Hi, I was just wondering how you could check to see if an try expression was not caught (no exeption). For example, if there is an error I want to print error, if not, print another message:

try {statement}
catch (Exception e) {ERROR statement}
"if not caught" {SUCCESS statement} --> Is this possible

thanks,
 
It's kind of a negative affirmation in your case. If the error doesn't print out--there wasn't one.
 
Does this work for you?

boolean ERROR=0;

try {statement}
catch (Exception e) {ERROR=1}
catch (Exception e) {ERROR=1}
catch (Exception e) {ERROR=1}
finally { if(!ERROR) {Success statement} }
 
In Java boolean values are true and false, never an int value. Try to compile this and see how far you get. BTW, the standard Java coding conventions call for all caps to only be used only for constant variables.
 
Here's an easier way:

try
{
statement
SUCCESS statement
}
catch (Exception e) {ERROR statement}

The success statement will only be reached if no exception is thrown.
 
You should not be doing this, generally.

That looks like procedural code to me.

use:

try{

}catch(Exception e)
{
... logging etc
... throw new AppException (); //User defined exception
//Should not swallow exceptions
}
// success code

You can have code after try catch block. Will execute on no on error condition if no rethrowing within catch block. Never swallow exceptions, unless it make sense. Error should be propagated to the level which should handle them.

HTH

Cal Cal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top