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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:11:33+00:00 2026-05-31T09:11:33+00:00

Is the following method of exception handling correct? I am finding it difficult to

  • 0

Is the following method of exception handling correct? I am finding it difficult to log the error and finally send the log file through email.
Where do I write code to log the error and send email?

A major problem I get is that when the exception is generated in SomeClass1 it is logged two times – the second time from SomeClass2. Is it a good idea to create a single custom exception type (SomeException in the example) and wrap the System.Exception whenever it occurs?

Also I am confused about how and when to show error message to the end user when we have a chain of try-catches calling one another.

class SomeClass1
{
    public static DataExtract(string sourcepath)
    {
        try
        {
            OleDbConnection oledb = new OleDbConnection();
            oledb.ConnectionString = "someconnectionstring";
            CompanyOLEDB.Open();
        }
        catch (Exception e)
        {
            throw new CustomException(e);
        }
    }
}

class SomeClass2
{
    private void SomeMethod()
    {
        try
        {
            // some code
            // some code

            SomeClass1.DataExtract()
        }
        catch (Exception e)
        {
            throw new CustomException(e);
        }
    }
}

public class CustomException : Exception
{
    protected CustomException() { }

    public CustomException(Exception e)
    {
        Log(e);
    }

    public CustomException(ExceptionType type)
    {
        this.Data.Add("Type", type);
        this.Data.Add("Message", "No message specified");
    }

    public CustomException(ExceptionType type, string message)
    {
        this.Data.Add("Type", type);
        this.Data.Add("Message", message);
    }

    public static void Log(Exception e)
    {
        System.IO.File.WriteAllText(Logfile.txt", e.ToString());
    }

    public static void Sendmail()
    {
        ExceptionMail.Sendmail();
    }
}
  • 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-31T09:11:34+00:00Added an answer on May 31, 2026 at 9:11 am

    Is the following method of exception handling correct?

    No. There are several issues. Here is the two most important ones.

    1. You should not catch exceptions that you can’t handle

    Quite important. All exception blocks that just rethrow or log exceptions clutter the code without adding any value. Only catch all exceptions in the topmost layer (in the webservice, the MVC controller, in a background thread etc) to prevent the application from crashing. (however, it’s sometimes better to let the application crash).

    An exception have been handled if the method can return the expected value.

    2. Always include the original exception

    You are hiding information that is important to be able to prevent the information in the future when you only copy partial information from the original exception.

    If you must catch/throw other you should do it like this:

    public class CustomException : Exception
    {
        public CustomException(string msg, Exception inner) : base(msg, inner){}
    }
    

    // and in a method:

    public void DoSomething()
    {
        try
        {
            SomeCoolOp();
        }
        catch (Exception err)
        {
            throw new CustomException("Tried to allocate some cool stuff", err);
        }
    }
    

    Changes compared to your code:

    1. We include the original exception in the way that Microsoft recommends
    2. We write a operation specific message, i.e. describing what we tried to do when the exception happened (instead of using the original message)

    More information

    I’ve written several blog posts about exception handling.

    You can start by reading: http://blog.gauffin.org/2010/11/do-not-catch-that-exception/

    And than read the rest: http://blog.gauffin.org/tag/exceptions/

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

Sidebar

Related Questions

The following method is launched from the constructor of UserControl. A cross thread exception
I am getting the following exception when i try to use the fetchall method
I'm using the following method to send mail from Python using SMTP. Is it
I'm having a little bit of trouble implementing the following method while handling the
During half a year of Winforms-MVP I designed the following exception handling strategy. I
How can I use exception handling in Rails? Currently I have done following. In
I see the need to copy and paste the following error handling code several
I'm getting na exception on the following method: @Transactional @RequestMapping(method=RequestMethod.PUT) public @ResponseBody Account create(@Valid
The following method does not work because the inner block declares a variable of
The following method does not compile. Visual Studio warns An out parameter may not

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.