c# - Catch exception thrown while task execution in anonymous method -


i have code similar :

class {     public action<int, int> onaddition;      public int add(int val1, int val2)     {         if (onaddition != null)         {             onaddition(val1, val2);         }          return val1 + val2;     } }  class b {     a = new a();      public b()     {         a.onaddition = (v1, v2) =>         {             task.run(() =>             {                 console.writeline("adding {0} , {1}", v1, v2);                 throw new exception("blah");             });         };     }      public int add(int val1, int val2)     {         return a.add(val1, val2);     } }  class program {     static void main(string[] args)     {         try         {             b b = new b();              console.writeline(b.add(1, 7));         }         catch (exception ex)         {             console.writeline(ex.message);         }     } } 

i have no control on classes a , b , problem when throw new exception("blah"); happens code not able catch exception.

how can code catch exception ?

you can't. if can't change code these classes can't catch exception.

you can notified subscribing taskscheduler.unobservedtaskexception event raised when gc collects task.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -