File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes explain the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "explain the output" Watch "explain the output" New topic

explain the output

rahul mehra
Ranch Hand

Joined: Aug 20, 2007
Posts: 33
class IntegerDivideByZeroErrorException extends Exception{}

class Average {
public static void main(String args[]){
} catch(IntegerDivideByZeroErrorException e)

System.out.println("exception handled in main!!!");

} finally{
System.out.println("finally done in main!!!");
public static void printAverage (int sum,int number)throws IntegerDivideByZeroErrorException
int average = computAverage(sum, number);

public static int computAverage (int sum,int number)throws IntegerDivideByZeroErrorException
System.out.println("computing average");
IntegerDivideByZeroErrorException n = new IntegerDivideByZeroErrorException();
throw n;
return sum/number;



the stacktrace should be printed between 'computing average' and 'exception handled in main'..... why is it getting printed at the last..irrespective of where i put the statement 'e.printStackTrace();'in the code..

computing average
exception handled in main!!!
finally done in main!!!
at Average.computAverage(
at Average.printAverage(
at Average.main(
BUILD SUCCESSFUL (total time: 0 seconds)

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
It's difficult to understand your program, because you did not UseCodeTags.

However, I think I can guess the problem. Your code prints its messages to System.out, but uses e.printStackTrace(). The method Throwable.printStackTrace() prints to System.err.

When output is sent to both System.out and System.err, and both those streams are connected to the console (as they are by default), you will get a random intermingling of the two streams. Therefore, stuff may not appear on the console in the order that it is output by the program.

I think changing your code to say e.printStackTrace(System.out) might fix it.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
rahul mehra
Ranch Hand

Joined: Aug 20, 2007
Posts: 33
I agree. Here's the link:
subject: explain the output
It's not a secret anymore!