2

I am talking about Try ... Catch ex as Exception .... End Try

If I pass exception to Error Logger, shall I pass exception object (ex) or ex.ToString ? Would I lose any information by passing ex.ToString?

Basically I was arguing with my manager, that ex.ToString should be excatly the same as passing ex as an object. M I wrong or right ?

7
  • Definitely you are wrong.. One more thing, never argue with your manager.. :D Oct 12, 2012 at 15:02
  • 3
    Use strings at the very last stage where you want to diplay,print or log something. The logger should decide when and how to convert the exception to a string. Oct 12, 2012 at 15:04
  • Haha .. well .. I said argue but I just argued very politely :D
    – Laurence
    Oct 12, 2012 at 15:05
  • Thanks Tim .. now I understand but I am struggling to decide which one to pick as the answer :D
    – Laurence
    Oct 12, 2012 at 15:06
  • @LaurenceNyein: If there are multiple answers that are equally good I generally sort by oldest and choose the first one that got posted. Its the fairest way to do it. Though of course +1 all the ones that you found helpful.
    – Chris
    Oct 12, 2012 at 15:08

4 Answers 4

6

One is an exception object, one is the string result of calling ToString on an exception object.

They are different types and have different information.

For logging purposes, there is little difference as the logger would normally call ToString on the exception object anyway.

However, it is better to centralize this - have the exception logger call ToString instead of doing this in every call site to the logger. It is more maintainable and opens you up for doing more things with the exceptions in the future if needed.

4

An exception object is different from calling .ToString() on the object itself. It depends on how much information you want to capture about the exception itself. If you just want the message, then calling .ToString() will be sufficient since that ends up giving you the message property from the exception object. The MSDN site lists different properties that belong to the base exception class. If you want to log any of these properties, then you will want the exception class and not just the message string.

Also note that different classes that inherit the base exception class provide additional information. As an example, the HttpException class provides additional properties. That information could be useful depending on what you need to see to troubleshoot.

1

An exception object will contain a lot more information than the result of the .ToString(). At the very least that information will be in a more useful format (ie you can get individual pieces easily).

If all you do in the logger is call exception.ToString() though without using anything else then currently it will be no real difference. However, passing the exception object is probably better for future proofing (ie if you start wanting some more properties of the exception).

For what its worth what ToString does is call the following overload passing true as the parameter:

Private Function ToString(ByVal needFileLineInfo As Boolean) As String
    Dim className As String
    Dim message As String = Me.Message
    If ((message Is Nothing) OrElse (message.Length <= 0)) Then
        className = Me.GetClassName
    Else
        className = (Me.GetClassName & ": " & message)
    End If
    If (Not Me._innerException Is Nothing) Then
        className = String.Concat(New String() { className, " ---> ", Me._innerException.ToString(needFileLineInfo), Environment.NewLine, "   ", Environment.GetRuntimeResourceString("Exception_EndOfInnerExceptionStack") })
    End If
    Dim stackTrace As String = Me.GetStackTrace(needFileLineInfo)
    If (Not stackTrace Is Nothing) Then
        className = (className & Environment.NewLine & stackTrace)
    End If
    Return className
End Function

As you can see it basically will output the class, message, innerexception.ToString() and stack trace.

0

Exception object is different than .ToString(). It has vast variety of properties, also if you need to pass the message only, you can use Ex.Message;

1
  • I'd strongly discourage using only Ex.Message @Zo Has. You lose the stack trace. Although that might be OK in rare cases, generally the stack trace is valuable and worthy of preservation. Mar 19, 2019 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.