This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
C:\Java\EigeneJavaProgramme>java FussyDivide1b java.lang.ArithmeticException: / by zero at FussyDivide1.fussyDivide(FussyDivide1b.java:7) at FussyDivide1.main(FussyDivide1b.java:13) Exception in thread "main"
Why ist there no Printout of the stacktrace where the exception occurred in FussyDivide1b as there is in FussyDivide? Why is there no Printout of the stacktrace when I throw my own exception?
C:\Java\EigeneJavaProgramme>java FussyDivide1b Nachricht: / by zero Thanks for your answers. Thomas.
First of all, java.lang.ArithmeticException is a RuntimeException. It means that it has not to be declared in the method signature even if it may happen (otherwise you would not have been able to compile your first exemple). Division by zero is one of them. In the second example you choose to mention in its signature that the method fussyDivide throws an ArithmeticException. But it is useless. The point is not "... when I throw my own exception?". You do not throw your own exception. The RuntimeException is thrown whatever in both cases.
Declaring public static int fussyDivide(int dividend, int divisor) throws ArithmeticException and public static int fussyDivide(int dividend, int divisor) is equivalent (except from a welcome documentation point of view). In your first example, you choose not to catch the exception. So your program does not know what to do when the Exception arises excpt tracing it down, from the main() to the division operation. In the second example you choose to handle the exception while catching it. It means that you are expecting that such a thing may happen and you propose a solution to handle the situation, namely writing a comment "System.out.println("Nachricht: "+ ae.getMessage());". Now that you have handle the exception, the execution may continue and there is no point tracing the exception.
You may force the trace if you want (with a Debug flag for example) if you put the line e.printStackTrace() in your try catch block. W. [ August 05, 2002: Message edited by: Wilfried LAURENT ]