RG21
RG21

Reputation: 53

WCF Issues with catching Inner Exception

I am trying to catch the inner exception of an exception and log it to a SQL Database table. The code I have catches and logs exceptions to the SQL table without issue, but I never get an inner exception record even when the message for the exception says "See inner exception for details." Can someone point me in the correct direction here as far as catching the inner exception is concerned? Here is the code that is catching the exception, but not working for the inner exception.

                try
                {
                    context.CalculateFreight(xmldoc);
                }
                catch (Exception ex)
                {
                    if (ex.Message.ToString().Length > 2048)
                    {
                        ApplicationError myError = new ApplicationError();
                        myError.ErrorNumber = 1;
                        myError.ErrorSeverity = 1;
                        myError.ErrorState = 1;
                        myError.ErrorLine = 29;
                        myError.ErrorProcedure = "Web Service - ProcessShipping()";
                        myError.ErrorMessage = ex.Message.ToString().Substring(0, 2047);
                        myError.ErrorDateTime = DateTime.Now;

                        context.ApplicationErrors.AddObject(myError);
                        context.SaveChanges();
                    }
                    else
                    {
                        ApplicationError myError = new ApplicationError();
                        myError.ErrorNumber = 1;
                        myError.ErrorSeverity = 1;
                        myError.ErrorState = 1;
                        myError.ErrorLine = 29;
                        myError.ErrorProcedure = "Web Service - ProcessShipping()";
                        myError.ErrorMessage = ex.Message.ToString();
                        myError.ErrorDateTime = DateTime.Now;

                        context.ApplicationErrors.AddObject(myError);
                        context.SaveChanges();
                    }

                    if (ex.InnerException.Message.ToString().Length > 0)
                    {
                        if (ex.InnerException.Message.ToString().Length > 2048)
                        {
                            ApplicationError myInnerException = new ApplicationError();
                            myInnerException.ErrorNumber = 1;
                            myInnerException.ErrorSeverity = 1;
                            myInnerException.ErrorState = 1;
                            myInnerException.ErrorLine = 29;
                            myInnerException.ErrorProcedure = "Web Service - INNER EXCEPTION";
                            myInnerException.ErrorMessage = ex.InnerException.Message.ToString().Substring(0, 2047);
                            myInnerException.ErrorDateTime = DateTime.Now;

                            context.ApplicationErrors.AddObject(myInnerException);
                            context.SaveChanges();
                        }
                        else
                        {
                            ApplicationError myInnerException = new ApplicationError();
                            myInnerException.ErrorNumber = 1;
                            myInnerException.ErrorSeverity = 1;
                            myInnerException.ErrorState = 1;
                            myInnerException.ErrorLine = 29;
                            myInnerException.ErrorProcedure = "Web Service - INNER EXCEPTION";
                            myInnerException.ErrorMessage = ex.InnerException.Message.ToString();
                            myInnerException.ErrorDateTime = DateTime.Now;

                            context.ApplicationErrors.AddObject(myInnerException);
                            context.SaveChanges();
                        }
                    }
                }

Upvotes: 0

Views: 815

Answers (1)

Steve Wellens
Steve Wellens

Reputation: 20640

If there's no inner exception, there's no inner exception.

From here: How to: Check an Exception's Inner Exception

If there is no original error, the value of InnerException will be a null reference or Nothing in Visual Basic.

The message "See inner exception for details." is probably someone's boilerplate text and not always applicable.

Upvotes: 1

Related Questions