• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic