Daniel Yochpaz
Daniel Yochpaz

Reputation: 13

Inheritance exception on exist exception

I have some functions that throw exceptions. I want that the code block that catch them also throw exception based on the exception that throw but with more parameters like ID and Note. how can I do that?

If any one can give me even direction it will be good!

Thanks (sorry for my English...)

Example for what I want (I know is not valid code...)

catch (Exception e)
{
    LogException l=e;
    l.Note = "note...";
    l.ID = 12;
    throw l;
}

Upvotes: 1

Views: 77

Answers (2)

Xilmiki
Xilmiki

Reputation: 1502

Try this.

try
{
    //....kaboom
}
catch (Exception ex)
{
    var newEX= new Exception("custom message", ex);
    newEX.Data.Add("any key",  "any obj");
    throw newEX;
}

Upvotes: 1

RosieC
RosieC

Reputation: 669

You would need to define your own exception (inherited from Exception) that had those extra properties. See here for the basic format a custom exception should take https://msdn.microsoft.com/en-us/library/ms229064(v=vs.100).aspx (Note that you should make the exception serializable.) you just add your extra properties.

Once you have created the exception class (LogException in your case) then your above code will work.

Upvotes: 2

Related Questions