Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass object (List<string>) as part of Exception

Tags:

I am constructing a list of strings and then want to throw an exception and let the UI handle the list and create the error message for the user.

Is there a way to do that?

like image 436
Pacman Avatar asked Jun 25 '13 16:06

Pacman


Video Answer


2 Answers

Exceptions contains Data property (which is a dictionary). It can be used to pass additional information:

try {     // throw new Exception } catch(Exception e) {     // whatever     e.Data["SomeData"] = new List<string>(); } 
like image 64
Zbigniew Avatar answered Oct 24 '22 18:10

Zbigniew


You can use the Exception.Data property to pass arbitrary data but a better (cleaner) solution would be to create your own custom exception class derived from Exception and add whatever properties you need to it.

Sample code:

public class MyException: Exception {     public List<String> MyStrings { get; private set; }      public MyException(List<String> myStrings)     {         this.MyStrings = myStrings;     } } 
like image 23
Z.D. Avatar answered Oct 24 '22 17:10

Z.D.