Exception.ToString()

P

Phill W.

Can anyone supply me with the code (C# or VB) that lies within the
Exception.ToString() method?

I'm writing Custom Exception classes and want to ensure that mine will
be consistent (as far as Logging goes) with those defined in the
Framework (particularly where Exceptions are nested).

So far, I've pulled mscorlib apart and rummaged around the IL Code
therein, and come up with this ...

Public Overrides Function ToString() As String
Dim sRC As String _
= Me.GetType().ToString() & ": " & Me.Message

If Not (Me.InnerException Is Nothing) Then
sRC &= " ---> " & Me.InnerException.ToString() _
& Environment.NewLine & " " _
& System.Environment.GetResourceString(
"Exception_EndOfInnerExceptionStack" )
End If
sRC &= Environment.NewLine & " " & Me.StackTrace
Return sRC
End Function

.... but the GetResourceString bit falls flat on its face and, to be
honest, I'm not entirely sure what it should be doing!

TIA,
Phill W.
 
J

Jay B. Harlow [MVP - Outlook]

Phill,
| Can anyone supply me with the code (C# or VB) that lies within the
| Exception.ToString() method?
I use this article for reference on Exceptions:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp08162001.asp


| ... but the GetResourceString bit falls flat on its face and, to be
| honest, I'm not entirely sure what it should be doing!
My local copy of MSDN only indicates .NET Compact Framework 2.0 (no mention
of other varieties of the framework). Which indicates to me that the
function is only available on the compact version for its limited support of
resources...

Normally in VB 2005 (.NET 2.0) I add a new resource file (.resx) to my
project, then use My.Resources to access it.

For example if I add the string to the Resources page of the project
Properties I can:

Dim s As String = My.Resources.Exception_EndOfInnerExceptionStack


Similar if I add Exceptions.resx to my VB project, with a string named
EndOfInnerExceptionStack, I can:

Dim s As String = My.Resources.Exceptions.EndOfInnerExceptionStack

I normally group my resources into common resource files. For example
Exceptions.resx would have all my exception text. FWIW: String.Format is
useful for text in resource files...

-- Exceptions.resx:
FileNotFound = "The file {0} was not found in folder {1}"

-- SomeWhere.vb

Dim theFileName As String = ...
Dim theFolderName As String = ...
Dim s As String =
String.Format(My.Resources.Exceptions.FileNotFound, theFileName,
theFolderName)

Then when it gets translated into a foriegn language the order of the
markers can be changed:

-- Exceptions.xx.resx:
FileNotFound = "{1} does not contain {0}"


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Can anyone supply me with the code (C# or VB) that lies within the
| Exception.ToString() method?
|
| I'm writing Custom Exception classes and want to ensure that mine will
| be consistent (as far as Logging goes) with those defined in the
| Framework (particularly where Exceptions are nested).
|
| So far, I've pulled mscorlib apart and rummaged around the IL Code
| therein, and come up with this ...
|
| Public Overrides Function ToString() As String
| Dim sRC As String _
| = Me.GetType().ToString() & ": " & Me.Message
|
| If Not (Me.InnerException Is Nothing) Then
| sRC &= " ---> " & Me.InnerException.ToString() _
| & Environment.NewLine & " " _
| & System.Environment.GetResourceString(
| "Exception_EndOfInnerExceptionStack" )
| End If
| sRC &= Environment.NewLine & " " & Me.StackTrace
| Return sRC
| End Function
|
| ... but the GetResourceString bit falls flat on its face and, to be
| honest, I'm not entirely sure what it should be doing!
|
| TIA,
| Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top