How can i change the Message
of an Exception object in C#?
The Message
property of Exception
is read-only:
public virtual string Message { get; }
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
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.
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.
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.
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.
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)
.
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.
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