Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Exception printing nitpicking (Sybex CSG 11 p.438 Kindle Edition)

 
Ranch Hand
Posts: 41
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While reading a chapter about printing an exception I played around with a code that prints the stack trace.
So instead of throwing and catching an exception, I created it and tried to display the stack trace of this, not yet fired, exception.
And the stack trace begins when the exception is instantiated. To my surprise -  when this exception is thrown, this still holds true.
Which is ok I guess, since the exception is ought to be immutable I believe.

Anyway, the text in the chapter goes like this:

"The stack trace is usually the most helpful one because it is a picture in time the moment the exception is thrown."

Boyarsky, Jeanne; Selikoff, Scott. OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide (p. 438). Wiley. Kindle Edition.

But this is actually not true. Stack trace shows the stack at the moment the exception is instantiated - not thrown!
Of course, since we all use throw new idiom so much, it is basically the same thing.

Here's a quick proof of this:



when executed, prints this:

 
author & internet detective
Posts: 40359
820
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting!
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic