3

I have a wcf service, for which I now have made a change to keep compiling my exceptions when they occur into a list, and at the end, throw this list as an Error response. Now single errors is fine. I'm able to throw that and get the output. But throwing the list as an error is proving to be a little challenging.

-What I have tried till now: using AggregateException. I tried sniffing around for a proper implementation. So far, came up with this:

throw new AggregateException("Multiple Errors Occured", ExceptionsList) 

Also,

List<CustomExceptionObject> ThrowException(List<CustomExceptionObject> ExceptionList) { AggregateException eggExp = new AggregateException(ExceptionList); throw new eggExp; } 

I keep getting Unhandled Exception error using these method. Any insight is appreciated.

UPDATE: The error message that I keep getting is -

An exception of type 'System.AggregateException' occurred in XYZ.dll but was not handled in user code. 

Keep in mind that throwing a single object of my CustomExceptionObject throws a proper error response in SOAP UI. I can't seem to pull of a list of these exception.

2
  • What's the message of the Unhandled Exception? Commented Jun 27, 2017 at 12:19
  • Unhandled is when you haven't try/catched in anywhere, and it reaches the top of the stack Commented Jun 27, 2017 at 12:33

3 Answers 3

6

An aggregate exception is the proper way to throw an exception that is a collection of exceptions.

throw new AggregateException("Multiple Errors Occured", ExceptionsList) 
Sign up to request clarification or add additional context in comments.

3 Comments

this is a comment not an answer
Please share the implementation?
I updated it with the implementation that Abhid had. An aggregate exception is the proper way to throw an exception that is a collection of exceptions.
3

You could use this pattern:

public void DoStuff() { var ex = new List<Exception>(); try { DoSomethingThatThrowsFooException(); DoSomethingElseThatThrowsFooException(); DoSomethingThatThrowsBarException(); } cath(FooException e) { ex.Add(e); } if (ex.Count>0) throw new AggregateException(ex); } 

The BarException will not be caught and not be included in the AggregateException. Ultimately it could lead to an UnhandledException if not caught anywhere else.

4 Comments

I seem to be doing something like that and yet I get the exception. What could I be doing wrong. Sigh...
My guess is that you're not catching the right types of exceptions
I want to throw the list of exceptions. This is happening in my handler method.
(I know this is already 2 years old at the time i post this comment but still) isn't try-catch block will stop whenever an Exception occurred ?, so in this example, the block will always only catch one exception if any, because the next step or call won't be invoked, right ?
0

So I overcame the issue with a hack. I'm not sure if it is the BEST way. But to get to the desirable outcome that I need, this works just fine.

STEP 1: Create a class that has a property of List<CustomExceptionObject> type.

STEP 2: Now when you have to throw an exception, Set this property with the incoming list, and cast it as a FaultException type, complete with some dummy FaultReason and FaultCode.

throw new FaultException<ListOfExceptionsClass>(listofExceptions, new FaultReason("Here is the List of All exceptions"), Faultcode); 

This will give a Fault Object in the response with a neat list of all exceptions occurred.

2 Comments

Hope this helps people with similar issues.
For anybody coming along here in the future, bare in mind that ListOfExceptionClass must be defined in a common library, or contract, between the server and client. Ideally you wouldn't return the service exceptions because you're exposing service information - but a FaultException is the correct way to return exceptions back to a client in WCF.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.