Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5928805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:20:10+00:00 2026-05-22T14:20:10+00:00

this case is bothering me since morning. Is it good practice to call garbage

  • 0

this case is bothering me since morning. Is it good practice to call garbage collector in class constructor if it throws Exception ? I have something like this:

public MyClass(/* some arguments */)
{
    try 
    {
      //do stuff...
    } catch(Exception e) {
      //do stuff, save logfile
      GC.SuppressFinalize(this);
    }

}

The reason that I did it like this is that if it threw Exception (usualy NullreferenceException) I want to log it in text file and I don’t need/want this object anymore. but is it good practice? If not how to do it properly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:20:10+00:00Added an answer on May 22, 2026 at 2:20 pm

    Your code doesn’t call the garbage collector – it merely suppresses the finalizer, which is only important if your class has a finalizer, which is pretty unlikely.

    It’s fair enough to log the exception, but currently you’re just catching it, which means the constructor will return with no errors. That’s almost certainly not a good idea. I suggest you probably want:

    try 
    {
      //do stuff...
    } catch(Exception e) {
      //do stuff, save logfile
      throw;
    }
    

    Having said that, I would normally try to centralize exception handling anyway, putting it a long way up the call stack and putting all logging in there. It would be pretty rare to put the logging within a constructor, IMO.

    Unless you’ve published the this reference from within your constructor, the newly created object will be eligible for garbage collection anyway – you don’t need to do anything else. If you implement IDisposable in your class you should be careful to release any resources you’ve already required within the constructor, but in most cases you can just let the exception bubble up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just seen this case class in the Scala actors package: case class !
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
Consider this case: public class SomeClass { public void someMethod() { new SomeInterface() {
In this case I have XML data source and external images files whole together
This is my 960 grid system case: <div class="kundregister_grid_full"> <div class="kundregister_grid_1">ID</div> <div class="kundregister_grid_1">Namn</div> <div
Lets say that in this case i have 4 divs called .CountThese and i
I think in this case there is no need to declare a public constructor
I have this: case true: echo '<textarea rows=2 cols=35 name=message_friend id=message_friend></textarea>'; break; default: echo
in this case I just wasn't sure what to look for. I have an
In this case I have a neural network (NN) instance in my base workspace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.