This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When is Stacktrace printed in Exceptions?

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic