c# - Exception is not bubbling up -


i'm trying catch thrown exception, doesn't bubble called. breaks in insertnewuser's catch block, saying

"an exception of type 'system.exception' occurred in peakpos.exe not handled in user code"

if click on debugger continue, goes file called app.g.i.cs , breaks on line don't understand has debugging on break. application terminates after that.

why saying exception unhandled when being rethrown , re-caught , handled (to-be handled)?


accessviewmodel.cs

public void savenewuser(popup popup) {     useraccounts.add(tempuser);      string salt = peakcrypto.generatesalt();     string hash = peakcrypto.generatehashedpassword(password + salt);     try     {         peakdb.insertnewuser(tempuser, salt, hash);     }     catch (exception e)     {         //todo notify user new account not saved     }      createnewaccount();      if (popup != null)         popup.isopen = false; } 

peakdb.cs

public static async void insertnewuser(useraccount user, string salt, string hash) {     var db = await databasehelper.getdatabaseasync();      try     {         using (var userstatement = await db.preparestatementasync(             "insert accessaccounts (firstname, lastname, salt, hash) values(@first, @last, @salt, @hash)"))         {             userstatement.bindtextparameterwithname("@first", user.firstname);             userstatement.bindtextparameterwithname("@last", user.lastname);             userstatement.bindtextparameterwithname("@salt", salt);             userstatement.bindtextparameterwithname("@hash", hash);             await userstatement.stepasync();         }     }     catch(exception e)     {         // todo: log exception error         throw;     } } 

app.g.i.cs

#if debug && !disable_xaml_generated_break_on_unhandled_exception     unhandledexception += (sender, e) =>     {         if (global::system.diagnostics.debugger.isattached) global::system.diagnostics.debugger.break();     }; #endif 

this expected behavior async operation. code handles/catches exceptions thrown synchronous part of method, lets application wide handle deal async part.

you can observe behavior expect if explicitly throw exception on first line of insertnewuser method (synchronous part).

fix: await async method.

// must return @ least `task` awaitable public static async task insertnewuser(... 

and await method (note "async viral" - async/await best practices):

   try    {         await peakdb.insertnewuser(tempuser, salt, hash);     }     catch (exception e) ... 

or @ least .wait if console app (wpf/winform/asp.net deadlock - await vs task.wait - deadlock?):

   try    {         peakdb.insertnewuser(tempuser, salt, hash).wait();     }     catch (exception e) ... 

if can't either - @ least use proper fire-and-forget async vs "old async delegate" call async void methods.

note: async void bad practice , should used form events.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -