Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Exception Object

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What happens to the exception object once the exception is caught or thrown? Is it garbage collected??
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swats,

Please take a look at the JavaRanch naming policy. You can update your publicly displayed name to conform to our policy by clicking here.

Thank you.

With regards to your question, exception objects will be eligible for garbage collection as soon as they are no longer used, just like any other object. So, for example, if you catch an exception and handle it, the exception will be eligible for garbage collection following the catch block (assuming you didn't assign any references to it to other variables).
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Caught: garbage collected at end of catch block.
Thrown: garbage collected at end of block where it's finally caught.

The Exception object is created within the catch block and its lifetime ends at the end of the block. It's similar to the lifetime of a variable in an initialized for loop:


The lifetime of i is the block in which it is used.

An exception



The exc object is created in the catch block and its lifetime is that block. Therefore it's free for garbage collection as the block finishes.
[ October 26, 2004: Message edited by: Louie van Bommel ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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