The simplest way
VaR T = new task () => {Throw new exception ("unknow excption") ;}); T. start (); try {T. wait ();} catch (aggresponexception e) {foreach (VAR item in E. innerexceptions) {console. writeline ("exception Type \ t {0} \ n from \ t {1} \ n exception content \ t {2}", item. getType (), item. source, item. message );}}
Disadvantage: This will block the current thread. Below is the domestic version
VaR T = new task () => {Throw new exception ("unknow excption") ;}); T. start (); var cat = T. continuewith (task => {foreach (VAR item in task. exception. innerexceptions) {console. writeline ("exception Type \ t {0} \ n from \ t {1} \ n exception content \ t {2}", item. getType (), item. source, item. message) ;}}, taskcontinuationoptions. onlyonfaulted); // specifies that the task console should be continued only when the task before the continuation task raises an unhandled exception. writeline ("main thread exited"); console. readkey ();
Disadvantage: the exception does not return to the main thread. Continue improvement
VaR T = new task () => {Throw new exception ("unknow excption") ;}); T. start (); var cat = T. continuewith (task => {Throw task. exception;}, taskcontinuationoptions. onlyonfaulted); // specifies that the task console should be continued only when the task before the continuation task raises an unhandled exception. writeline ("main thread exited"); thread. sleep (1000); // Exception Handling try {cat. wait ();} catch (aggresponexception e) {foreach (VAR item in E. innerexceptions) {console. writeline ("exception Type \ t {0} \ n from \ t {1} \ n exception content \ t {2}", item. innerexception. getType (), item. innerexception. source, item. innerexception. message );}}
Exception Handling in task