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 9285153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:05:26+00:00 2026-06-18T19:05:26+00:00

I am creating a test logger which will include information about any exceptions that

  • 0

I am creating a test logger which will include information about any exceptions that are thrown so that they can be displayed properly using a GUI.

Is it safe to use the following construct:

public class TestLogEntry {
    public System.Exception Exception { get; private set; }

    public TestLogEntry(/*...,*/ System.Exception exception = null) {
        //...

        Exception = exception;
    }
}

Or would the following be better?

public class TestLogEntry {
    public string StackTrace { get; private set; }
    public System.Type ExceptionType { get; private set; }
    public string ExceptionMessage { get; private set; }

    public TestLogEntry(/*...,*/ System.Exception exception = null) {
        //...

        if (exception != null) {
            StackTrace = exception.StackTrace;
            ExceptionType = exception.GetType();
            ExceptionMessage = exception.Message;
        }
    }
}

Whilst the first approach is more flexible (because it can contain additional custom data), here are my concerns:

  • Holding on to exception object for a prolonged period.
  • Using a lot of memory if exception objects are preventing large objects from being garbage collected.
  • Exception returning wrong values when accessed out of context.

Q1. Are the above concerns valid?

Q2. Do exception objects typically reference a lot of other data?

  • 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-06-18T19:05:27+00:00Added an answer on June 18, 2026 at 7:05 pm

    Your concerns are valid: exception objects might hold onto arbitrary other objects. This is very rare in practice. Actually I have never seen the Exception.Data property used. But I have seen an Exception derived class hold onto something big using custom fields: WebException has a WebResponse property!

    So you see you might be keeping alive even something as expensive as an unmanaged resource.

    I’d actually copy out the information and discard the Exception. Remember to also copy the InnerException.

    A different concern might be that Exception is a mutable type. You can throw it at any time altering its stack trace. I’d like to capture its state just for that reason.

    Also important is memory usage. Exception has some fields that are probably never used. You can save them. Also, inlining its fields into your log message object will eliminate the object header and object reference. A small gain but maybe worth taking in case of frequent exceptions.

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

Sidebar

Related Questions

I am creating a logging object that will take all logs sent to it
I am creating test.js from Java, as per below. Test.js implements function d(), that
I am creating a unit test that invokes a create task method. The create
I'm creating test data for a Rails app. How do I define the value
I have the following couple of C pre-processor macros for creating test functions: //
I am creating a test bench for a BCD_counter. When i try to compile
I am creating Junit test cases in Spring. private static BeanFactory servicefactory = null;
I've created a small script to test creating my own help based powershell and
I am creating a Menu test for my restaurant as a project and I
I am creating a custom test runner for JUnit test cases using JUnitCore's run(junit.framework.Test

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.