Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net - How to change exception message of Exception object?

Tags:

.net

exception

How can i change the Message of an Exception object in C#?

Bonus Chatter

The Message property of Exception is read-only:

public virtual string Message { get; } 

Additional Reading

The same question, in PHP, was answered, "You can't", but gave a workaround:

You can however determine it's class name and code, and throw a new one, of the same class, with same code, but with different message.

How can i determine an exception's class name, and throw a new one of the same class, but with a different message, in C#?

e.g.:

catch (Exception e) {    Exception e2 = Activator.CreateInstance(e.GetType());    throw e2; } 

doesn't work because the Message property of an exception is read-only and .NET. See original question.


Update

i tried catching each type of exception i expect:

try {     reader.Read(); } catch (OleDbException e) {    throw new OleDbException(e, sql); } catch (SqlException e) {    throw new SqlException (e, sql); } catch (IBM.DbException e) {    throw new IBM.DbException(e, sql); } catch (OdbcException e) {    throw new OdbcException (e, sql); } catch (OracleException e) {    throw new OracleException (e, sql); } 

Except that now my code forces a dependency on assemblies that won't be present in every solution.

Also, now the exception seems to come from my code, rather than the line that threw it; i lose the exception's location information

like image 390
Ian Boyd Avatar asked Feb 17 '12 19:02

Ian Boyd


People also ask

How do I write an exception message?

Use grammatically correct error messagesWrite clear sentences and include ending punctuation. Each sentence in the string assigned to the Exception. Message property should end in a period. For example, "The log table has overflowed." would be an appropriate message string.

How do I return an exception message in C#?

Throw the exception, have a try/catch block in the code that calls your method, and show the message box from the catch block. In fact, don't even have a try/catch block or any return values in your createFolding() method at all.

How do I show exception messages in .NET core?

To handle exceptions and display user friendly messages, we need to install Microsoft. AspNetCore. Diagnostics NuGet package and add middleware in the Configure() method. If you are using Visual Studio templates to create ASP.NET Core application then this package might be already installed.

How do you handle custom exceptions in C#?

In C# there are three keywords Try, Catch, and Finally for handling exceptions. In try block statements it might throw an exception whereas catch handles that caused by try block if one exists. The finally block is used for doing any clean up process. The statement in finally block always executes.


2 Answers

You create a new Exception (or – better – specific subtype) that has the new message (and pass the original exception as InnerException).

Eg.

throw new MyExceptionType("Some interesting message", originalException); 

NB. If you really want to use Activator.CreateInstance you can use an overload that can be passed parameters, but different Exception derived types cannot be relied on to have an constructor overload for (message, innerException).

like image 85
Richard Avatar answered Oct 06 '22 15:10

Richard


i found the solution in a blog post linked from a news site:

catch (Exception e) {    Exception e2 = (Exception)Activator.CreateInstance(e.GetType(), message, e);    throw e2; } 

It's not perfect (you lose your stack trace); but that's the nature of .NET.

like image 37
Ian Boyd Avatar answered Oct 06 '22 14:10

Ian Boyd