wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Q" Watch "Exception Q" New topic

Exception Q

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Consider the piece of code given below:

Computing average.
Finally done.
<Stack Trace is printed.>

My question: Why isn't the statement marked //2 part of the o/p, since the exception is handled by the default exception handler (the main() method) ?

[ February 28, 2005: Message edited by: Barry Gaunt ]
[ February 28, 2005: Message edited by: Kedar Dravid ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please use tags around your code. Thanks

Also the code does not compile.
[ February 28, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
main() is throwing the exceptiom, not handling it. The JVM is where the catching of the exception is done. Look at the stack trace carefully.
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
Your code has few problems, I don't know how you got this output.
1. It is not compiling as totalNumber is not defined, I think totalNumber should be replaced with totalAverage.
2. It is not handling ArithmeticException.

I have above changes to your code and now it gives your desired result. Here is the changed code :
public class Average
public static void main(String[] args)
printAverage(100, 0);
System.out.println("Exit main()."); //2

public static void printAverage(int totalSum, int totalAverage)
int average = computeAverage(totalSum, totalAverage);
System.out.println("Average = " + totalSum + " / " + totalAverage + " = " + average);
catch ( ArithmeticException a ) {}
System.out.println("Finally done.");
System.out.println("Exit printAverage.");

public static int computeAverage(int sum, int number)
System.out.println("Computing average.");
return sum/number;

Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
By handling, it means catching the exception using try/catch statement.

wood burning stoves
subject: Exception Q
Similar Threads
How to write an algorithm for an average of values?
multiples project no output
return statement effect in Exception class
finally block with return statement
explain the output