debugcsharpMinor
Accumulating inner exception messages
Viewed 0 times
inneraccumulatingmessagesexception
Problem
Say, there is a class with some methods, that have
I came up with two possible solutions:
Code snippet:
Which one of them would be better, or is there another way of doing it, or maybe this approach is not good at all?
try catch blocks. I need to accumulate messages from all inner exceptions of generated exception and use throw new Exception(exceptionMessages). I came up with two possible solutions:
- Create a private method, concatenate inner exception messages, return a
stringand raise an exception in a caller method. (SomeMethod1below);
- Create a private method, concatenate inner exception messages and raise exception inside it. (
SomeMethod2below);
Code snippet:
class SomeClass
{
private void _raiseException(Exception ex)
{
var exceptionMessages = new StringBuilder();
do
{
exceptionMessages.Append(ex.Message);
ex = ex.InnerException;
}
while (ex != null);
throw new Exception(exceptionMessages.ToString());
}
private string _getInnerExceptions(Exception ex)
{
var exceptionMessages = new StringBuilder();
do
{
exceptionMessages.Append(ex.Message);
ex = ex.InnerException;
}
while (ex != null);
return exceptionMessages.ToString();
}
public void SomeMethod1()
{
try
{
//some code to try
}
catch (Exception e)
{
throw new Exception(this._getInnerExceptions(e));
}
}
public void SomeMethod2()
{
try
{
//some code to try
}
catch (Exception e)
{
this._raiseException(e);
}
}
}Which one of them would be better, or is there another way of doing it, or maybe this approach is not good at all?
Solution
Svick is right
Use the AggregateException class to collect all exceptions you need. With AggregateException everyone can prepare it's own code to handle the multiple exception situation but with your original solution they have to handle a huge string in a stock exception class. How can be this usefull?
Storing exception.Message
It's bad. If you store only the exceptions' messages you will loose a lot of information and the types of the exceptions. How would you handle an exception if only the Message is what you have? Parsing it? And we havent talked about the specific properties of specific exceptions like some validation exception which can contain the unvalid property name and value for an object.
Use the AggregateException class to collect all exceptions you need. With AggregateException everyone can prepare it's own code to handle the multiple exception situation but with your original solution they have to handle a huge string in a stock exception class. How can be this usefull?
Storing exception.Message
It's bad. If you store only the exceptions' messages you will loose a lot of information and the types of the exceptions. How would you handle an exception if only the Message is what you have? Parsing it? And we havent talked about the specific properties of specific exceptions like some validation exception which can contain the unvalid property name and value for an object.
Context
StackExchange Code Review Q#25391, answer score: 6
Revisions (0)
No revisions yet.