c# - Exception not caught in Async / Await block -


i'm learning use async / await in windows forms application, trying keep windows application responsive while doing slow actions. see difference in handling of thrown exceptions.

if use webclient.downloadstringtaskasync, exceptions caught code:

private async void button1_click(object sender, eventargs e) {   try   {     this.textbox1.text = await webclient.downloadstringtaskasync("invalid address");   }   catch (exception exc)   {     textbox1.text = exc.message;   } } 

however, if call async function in own code exception not caught:

private string gettext() {   throw new exception("tough luck!"); }  private async void button3_click(object sender, eventargs e) {   using (var webclient = new webclient())   {     try     {       this.textbox1.text = await task.run(() => gettext());     }     catch (exception exc)     {       textbox1.text = exc.message;     }   } } 

as answer stackoverflow question correct way throw , catch exceptions using async/await advised "disable 'just code' in tools->options->debugging->general"

after comments daniel hilgarth (thanks daniel!), saw copy - paste error. i've corrected here. problem still remains, if follow advise disable "just code", exception caught.

so guess question solved.

the problem this:
throwing exception catching webexception.

two solutions:

  1. throw webexception
  2. catch exception

solution 1 preferred because should never throw unspecific exception.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -