aspose file tools*
The moose likes Beginning Java and the fly likes When is Stacktrace printed in Exceptions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When is Stacktrace printed in Exceptions?" Watch "When is Stacktrace printed in Exceptions?" New topic
Author

When is Stacktrace printed in Exceptions?

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Unformattierte Ausgabe der Arithmethic Exception


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.
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
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 ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
A stack trace is printed by default by the JVM if the exception is not handled. You can cause it to be printed in the catch statement by using
e.printStackTrace ;


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When is Stacktrace printed in Exceptions?