locked
Exception Handling RRS feed

  • Question

  • Ok, I need to understand exception handling better.  Mainly when should I use a Try Catch vs. something like this in a constructor which doesn't required a Try Catch:

    public MyClass
    {
        public MyClass(string stringA, int intA, bool boolA)
        {
            if(string.IsNullOrEmpty(stringA))
                {throw new NullReferenceException("name is null or empty");}
            ...
         }

    So I don't get when to use a Try/Catch and handle something vs. just throwing an exception like this.  What happens when you just throw an exception like this in a constructor?  Why don't I need to use a Try/Catch?  Just trying to understand the fundamentals of exception handling.

    }
    C# Web Developer
    Monday, June 22, 2009 9:43 PM

Answers

  • In addition, you do not want to use exception handling to do "normal" processing.

    For example, avoid doing things like:

    Int32 x = Int32.MinValue;
    try
    {
       x = Convert.ToInt32(someStringValue);
    }
    catch (Exception ex)
    {
       x = 0;
    }

    In this example, there is a better way of checking to see if the string can be converted to a number that will NOT throw an exception. Exceptions are somewhat expensive, and should only be raised in, well, exceptional cases.

    As to catching exceptions I follow the guideline that you only handle an exception if you can actually do something with it. For example, I might write my DAL to handle SQL Server exceptions by logging them, then translating them (turning them into another exception).

    For example:

    try
    {
       // Do something that may throw an exception
    }
    catch (SqlException sqlEx)
    {
       // Translate the exception to something more friendly to your BL or UI.
       throw new MyCustomException("A data error occurred: " + sqlEx.Message);
    }

    Check out: http://msdn.microsoft.com/en-us/library/8ey5ey87(VS.71).aspx and let us know if you have any more questions.


    Chris Snyder, Stumbling through code one day at a time If your question was answered, please mark it.
    • Marked as answer by NoEgo Tuesday, June 23, 2009 4:02 PM
    Tuesday, June 23, 2009 12:28 AM
  • Hi NoEgo,

     

    Here are some additional references about the exceptions in .NET:

     

    The Design Guidelines for Exceptions in MSDN documentation, including Exception Throwing and Handling, Standard Exception Types, Custom Exceptions and Exceptions Performances.

    http://msdn.microsoft.com/en-us/library/ms229014.aspx

     

    Exception Handling Best Practices in .NET (CodeProject):

    http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx

     

    First and second chance exception handling:

    http://support.microsoft.com/kb/105675

     

     

    Besides, you are throwing the exception in the constructor.   It is fine based on knowledge.  However, it is not recommended to throw exceptions in cctor according to this blog, http://blogs.msdn.com/cbrumme/archive/2003/04/15/51348.aspx.

     

     

     

    If you have any questions or concerns, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Marked as answer by NoEgo Tuesday, June 23, 2009 4:02 PM
    Tuesday, June 23, 2009 6:59 AM
  • Any time you want to catch an exception, you should use the Try Catch.

    In your example, you are throwing an error in the constructor, therefore when you construct an instance of this class, you will want a Try/Catch block to catch this error that you are throwing.

    If you don't have a Try/Catch block on the code that constructs the instance, the error will propagate up your call stack. If there is no Try/Catch somewhere up the call stack, you will get an untrapped error in your code.

    Make sense?
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoEgo Tuesday, June 23, 2009 4:01 PM
    Monday, June 22, 2009 9:51 PM

All replies

  • Any time you want to catch an exception, you should use the Try Catch.

    In your example, you are throwing an error in the constructor, therefore when you construct an instance of this class, you will want a Try/Catch block to catch this error that you are throwing.

    If you don't have a Try/Catch block on the code that constructs the instance, the error will propagate up your call stack. If there is no Try/Catch somewhere up the call stack, you will get an untrapped error in your code.

    Make sense?
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoEgo Tuesday, June 23, 2009 4:01 PM
    Monday, June 22, 2009 9:51 PM
  • In addition, you do not want to use exception handling to do "normal" processing.

    For example, avoid doing things like:

    Int32 x = Int32.MinValue;
    try
    {
       x = Convert.ToInt32(someStringValue);
    }
    catch (Exception ex)
    {
       x = 0;
    }

    In this example, there is a better way of checking to see if the string can be converted to a number that will NOT throw an exception. Exceptions are somewhat expensive, and should only be raised in, well, exceptional cases.

    As to catching exceptions I follow the guideline that you only handle an exception if you can actually do something with it. For example, I might write my DAL to handle SQL Server exceptions by logging them, then translating them (turning them into another exception).

    For example:

    try
    {
       // Do something that may throw an exception
    }
    catch (SqlException sqlEx)
    {
       // Translate the exception to something more friendly to your BL or UI.
       throw new MyCustomException("A data error occurred: " + sqlEx.Message);
    }

    Check out: http://msdn.microsoft.com/en-us/library/8ey5ey87(VS.71).aspx and let us know if you have any more questions.


    Chris Snyder, Stumbling through code one day at a time If your question was answered, please mark it.
    • Marked as answer by NoEgo Tuesday, June 23, 2009 4:02 PM
    Tuesday, June 23, 2009 12:28 AM
  • Hi NoEgo,

     

    Here are some additional references about the exceptions in .NET:

     

    The Design Guidelines for Exceptions in MSDN documentation, including Exception Throwing and Handling, Standard Exception Types, Custom Exceptions and Exceptions Performances.

    http://msdn.microsoft.com/en-us/library/ms229014.aspx

     

    Exception Handling Best Practices in .NET (CodeProject):

    http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx

     

    First and second chance exception handling:

    http://support.microsoft.com/kb/105675

     

     

    Besides, you are throwing the exception in the constructor.   It is fine based on knowledge.  However, it is not recommended to throw exceptions in cctor according to this blog, http://blogs.msdn.com/cbrumme/archive/2003/04/15/51348.aspx.

     

     

     

    If you have any questions or concerns, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Marked as answer by NoEgo Tuesday, June 23, 2009 4:02 PM
    Tuesday, June 23, 2009 6:59 AM