Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception message

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, not sure why this code when it throws an exception prints the stack trace:



This is what I get as output:

Let me see first if I get things right. The ArrayIndexOutOfBoundsException exception isn't caught by the catch statement with MyException, so is it for that reason that I get a full stack trace?
 
Henry Wong
author
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:
Let me see first if I get things right. The ArrayIndexOutOfBoundsException exception isn't caught by the catch statement with MyException, so is it for that reason that I get a full stack trace?


The behavior of the JVM (ran from the command line), if it gets an exception thrown out of the main() method, is to print the exception, along with a stack trace.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I thought that you get a stack trace if you say something like exception.printStackTrace(), as opposed to System.out.println( exception). Let me give you another example, in this code instead


As output here I get only
But the exception got caught by the catch statement here, that's why I said whether the act that an exception is caught or not has an effect on what it's printed off. So, are we then saying that only because the first exception got caught outside the main method we got a stack trace and in the second one, since it got caught inside the main method we didn't get the stack trace?
 
Henry Wong
author
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the first example, the exception was not caught by your code, and hence, it was handled by the JVM, after the main() method return. In the second example, the exception was caught by your code, and hence, handled by your code.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right, so when the JVM deals with it the full stack trace is provided because I haven't essentially caught the exception?
 
Henry Wong
author
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:right, so when the JVM deals with it the full stack trace is provided because I haven't essentially caught the exception?


I don't know what you mean by "the JVM deals with it the full track trace is provided"? The stack trace is always available with the exception. If you like, your own code (in the second example) could have printed it -- just like the JVM default exception handler.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that what I meant to say was, if the exception is caught and there is a custom message to go with t, then only the exception and the message is printed, whereas, if the exception is not caught - like in the first example - then the stack trace is printed even thought I didn't explicitly ask for it to be printed off (and by explicitly I mean by saying use )
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to be absolutely clear, here are a few more examples.
Take this method:

This catches the ArrayIndexOutOfBoundsException and prints the message caught, nothing else.
This one instead

doesn't catch it of course so the output is a full stack trace even if I didn't really explicitly asked for it

 
Henry Wong
author
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:
doesn't catch it of course so the output is a full stack trace even if I didn't really explicitly asked for it



First of all, in your second case, since you don't actually have any code that is related to the index out of bounds exception, then technically, you "didn't asked for it" *and* "didn't [not] ask for it". It is merely the default handler that is running and producing the output.

Having said that, although I have *never* tried it with the main thread, there is an option to change the uncaught exception handler. Perhaps, you can look into that? ... or ... as you have seen, if you want a particular behavior with certain exceptions, it is possible (and likely easier) to catch and implement that behavior.

Henry 
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic