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!

unpredictable behavior (exceptions?)

Status
Not open for further replies.

benlogan1981

Programmer
Dec 9, 2001
14
GB
Guys and Gals...

Ive been programming in Java for years, but I have never really fully understood why some method execution, (or at least debugging statements to the console) seems to take place/appear almost at random.

This has been highlighted again recently with a small app im developing just to test my knowledge of Java5. Its basically a small collection of classes that test various bits of new functionality in Java5 and outputs my results to the console; just for me to check I know what I'm doing!

Anyway, the ambigious behavior surrounds the exception testing. So, i have this class/constructor that gets called at the same place for each execution of the app, yet the debug statements appear at random locations within the overall output.

Is this behavior all done to how the VM handles exceptions? Any help would be appreciated...this is not a problem as such, just something I want to understand better:)

public class ExceptionTest {

public ExceptionTest(){

try{
//int temp = 5/0;
throw new Exception();
//System.out.println("No Problem Matey!");
}
catch(Exception e){
System.out.println("Exception on its way!");
e.printStackTrace();
}
finally{
// this will happen whether there is an exception or not!
System.out.println("Exception or not, lets finish here!");
}
}
}

 
System.out is a buffered stream - so your debug statements may well appear "out of sync".

Using System.err should fix your odd behaviour.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top