I have a custom exception class which contains some additional fields. I want these to be written out in the ToString()
method, but if I implement my own ToString()
, I loose some other useful stuff (like writing the exception type name, the inner exception data and the stack trace).
What is the best way/pattern to implement your own ToString()
method for such exceptions? Ideally it should reuse the existing mechanism, but be formatted in way similar to the default ToString()
implementation.
UPDATE: prepending or appending my custom fields to the base.ToString() text isn't ideal IMHO, for example
PimTool.Utilities.OERestServiceUnavailableException: test ---> System.InvalidOperationException: inner message --- End of inner exception stack trace --- at PimTool.Tests.Services.OE.OERestClientTests.ExceptionsLogging() in D:\svn\NewPimTool\PimTool.Tests\Services\OE\OERestClientTests.cs:line 178, StatusCode=0, message='test', requestId='535345'
means the custom fields are written at the end of the (potentially long) exception description. On the other hand, I want the exception type to be the first information written in the description.
UPDATE 2: I've implemented a solution for this, look for my own answer below.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
This is all overkill. Your exception should just override the Message Property.
public override String Message { get { return base.Message + String.Format(", HttpStatusCode={0}, RequestId='{1}'", httpStatusCode, RequestId); } }
The default ToString method for the Exception class is basically "ClassName: Message --> InnerException.ToString() StackTrace
". So overriding the Message puts your message text exactly where it should be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With