Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

explain the output

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class IntegerDivideByZeroErrorException extends Exception{}

class Average {
public static void main(String args[]){
try{
printAverage(100,0);
} catch(IntegerDivideByZeroErrorException e)
{
e.printStackTrace();

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");
if(number==0){
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..


compile:
run:
computing average
exception handled in main!!!
finally done in main!!!
IntegerDivideByZeroErrorException
at Average.computAverage(Average.java:26)
at Average.printAverage(Average.java:19)
at Average.main(Average.java:7)
BUILD SUCCESSFUL (total time: 0 seconds)
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
rahul mehra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks...
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic