locked
Error handling help RRS feed

  • Question

  • User-1188570427 posted

    How should I handle the below code?

    I need a try/catch exception around some code and I want to try catch the error as well.

    On the try/catch for the error, should I re throw the main exception or?

                try
                {
                    var testDate = new DateTime(2015, 45, 45);
                }
                catch (Exception ex)
                {
                    try
                    {
                        // Log error here
                    }
                    catch (Exception exError)
                    {
                        // Can I just re-throw ex here or? I don't want to ignore the error, but also need to try/catch the logging of my error handling
                        throw; // this will throw the exception with logging the error - Right?
    
                        throw ex; // This will throw the main error because the date is not valid
                    }
                }


    Saturday, June 2, 2018 9:58 PM

Answers

  • User753101303 posted

    Hi,

    And your extra logging code is called as a method? You should likely handle a possible exception inside your extra logging method rather than in each and every catch clause that will call this.

    Not directly related but I try to track the additional info I want as part of my usual flow and then my global handler will just dump this extra information if available when an exception happens.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 3, 2018 8:42 AM
  • User1120430333 posted

    DA924

    Maybe, you need to learn how to do global exception handling, which is one spot in code that all exceptions come to when there is an exception. The exception gets logged by the global code and a redirect to a user friendly page. It's better than having try/catches all over the code. 

    One doesn't throw exceptions on validation errors.

    I know about Global error handling, but that is NOT what I am trying to do.

    I need to have the try/catch around the code for extra logging purposes and I need to know what should happen on the try/catch on the code for logging the main error.

     try
     {
    
     }
     catch (ex)
     {
        log ex
        throw ex
     } 
        
    Some try/catch for the logging is superfluous, IMO. It would be logging to a text file on an append or something like Log4net, which could be some kind of common function. 
    
    However, a database table for logginbg could be used where you would use a try/catch  as it couldn't log,  and on the catch,  write to a log text file and then throw the SQL execption, which could be in a common function too.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 3, 2018 8:47 AM
  • User1120430333 posted

    https://airbrake.io/blog/dotnet/system-data-sqlclient-sqlexception

    some do a double Catch()

    catch(SqlExexception sqlex)

    catch(Excpetion ex)   

    They do the double in case the exception thrown is not a SQL exception.

    Some just use the catch on Exception only that catches all Exceptions no matter the type of Exception.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 4, 2018 7:11 AM

All replies

  • User1120430333 posted

    Maybe, you need to learn how to do global exception handling, which is one spot in code that all exceptions come to when there is an exception. The exception gets logged by the global code and a redirect to a user friendly page. It's better than having try/catches all over the code. 

    One doesn't throw exceptions on validation errors.

    Saturday, June 2, 2018 11:01 PM
  • User-1188570427 posted

    Maybe, you need to learn how to do global exception handling, which is one spot in code that all exceptions come to when there is an exception. The exception gets logged by the global code and a redirect to a user friendly page. It's better than having try/catches all over the code. 

    One doesn't throw exceptions on validation errors.

    I know about Global error handling, but that is NOT what I am trying to do.

    I need to have the try/catch around the code for extra logging purposes and I need to know what should happen on the try/catch on the code for logging the main error.

    Sunday, June 3, 2018 2:20 AM
  • User753101303 posted

    Hi,

    And your extra logging code is called as a method? You should likely handle a possible exception inside your extra logging method rather than in each and every catch clause that will call this.

    Not directly related but I try to track the additional info I want as part of my usual flow and then my global handler will just dump this extra information if available when an exception happens.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 3, 2018 8:42 AM
  • User1120430333 posted

    DA924

    Maybe, you need to learn how to do global exception handling, which is one spot in code that all exceptions come to when there is an exception. The exception gets logged by the global code and a redirect to a user friendly page. It's better than having try/catches all over the code. 

    One doesn't throw exceptions on validation errors.

    I know about Global error handling, but that is NOT what I am trying to do.

    I need to have the try/catch around the code for extra logging purposes and I need to know what should happen on the try/catch on the code for logging the main error.

     try
     {
    
     }
     catch (ex)
     {
        log ex
        throw ex
     } 
        
    Some try/catch for the logging is superfluous, IMO. It would be logging to a text file on an append or something like Log4net, which could be some kind of common function. 
    
    However, a database table for logginbg could be used where you would use a try/catch  as it couldn't log,  and on the catch,  write to a log text file and then throw the SQL execption, which could be in a common function too.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 3, 2018 8:47 AM
  • User-1188570427 posted

    Hi,

    And your extra logging code is called as a method? You should likely handle a possible exception inside your extra logging method rather than in each and every catch clause that will call this.

    Not directly related but I try to track the additional info I want as part of my usual flow and then my global handler will just dump this extra information if available when an exception happens.

    Can you show me an example of how you do it?

    My extra logging isn't handled inside a method because it is different for each method - make sense?

    So I set up a string builder that holds my extra info, then call a function that logs it to the database, and then throw a message up with the error etc. Then I have a try catch around all of that right now, but nothing in there. 

    Sunday, June 3, 2018 11:05 PM
  • User-1188570427 posted

    tvb2727

    DA924

    Maybe, you need to learn how to do global exception handling, which is one spot in code that all exceptions come to when there is an exception. The exception gets logged by the global code and a redirect to a user friendly page. It's better than having try/catches all over the code. 

    One doesn't throw exceptions on validation errors.

    I know about Global error handling, but that is NOT what I am trying to do.

    I need to have the try/catch around the code for extra logging purposes and I need to know what should happen on the try/catch on the code for logging the main error.

     try
     {
    
     }
     catch (ex)
     {
        log ex
        throw ex
     } 
        
    Some try/catch for the logging is superfluous, IMO. It would be logging to a text file on an append or something like Log4net, which could be some kind of common function. 
    
    However, a database table for logginbg could be used where you would use a try/catch  as it couldn't log,  and on the catch,  write to a log text file and then throw the SQL execption, which could be in a common function too.

    Can you give me an example of throwing the SQL exception?

    Sunday, June 3, 2018 11:06 PM
  • User1120430333 posted

    https://airbrake.io/blog/dotnet/system-data-sqlclient-sqlexception

    some do a double Catch()

    catch(SqlExexception sqlex)

    catch(Excpetion ex)   

    They do the double in case the exception thrown is not a SQL exception.

    Some just use the catch on Exception only that catches all Exceptions no matter the type of Exception.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 4, 2018 7:11 AM