c# - Action in Controller is not catching an exception -


i hope can explain happening.

i created repository class "invoicesrepository.cs" manage logic of listing, inserting, updating, deleting, etc. "invoice.cs" objects. cleaner , easier maintain.

public class invoicesrepository {     protected mysqlcontext db;      public invoicesrepository(mysqlcontext db)     {         this.db = db;     }      public async void insert(invoice obj)     {         this.db.invoices.add(obj);         await this.db.savechangesasync();          // performing other tasks...     }      // other useful methods... } 

there "invoicescontroller.cs" actions require. inside controller create "invoicetepository" obj , use save information database. and, in every action

public class invoicescontroller : controller {     private invoicesrepository invoices;      public invoicescontroller()     {         this.invoices = new invoicesrepository(new mysqlcontext());     }      [httppost]     public async task<actionresult> upload(invoice invoice)     {         try         {             this.invoices.insert(invoice);         }         catch (dbentityvalidationexception ex)         {             foreach (var eve in ex.entityvalidationerrors)             {                 foreach (var err in eve.validationerrors)                 {                     modelstate.addmodelerror(err.propertyname, err.errormessage);                 }             }         }         catch (system.data.entity.infrastructure.dbupdateexception ex)         {             modelstate.addmodelerror("", ex.tostring());         }         catch (system.data.entity.core.updateexception ex)         {             modelstate.addmodelerror("", ex.tostring());         }         catch (mysql.data.mysqlclient.mysqlexception ex)         {             modelstate.addmodelerror("", ex.tostring());         }         catch (exception ex)         {             modelstate.addmodelerror("", ex.tostring());         }          return view();     }      // other useful action methods... } 

for testing, i'm inserting "invoice" object has duplicate data (unique column) in database expecting throw exception , action catching , display error in view but... exception "thrown" not "catched".

i debugged see kind of exceptions thrown (including inner exceptions) , added required "catches" still exception not "catched".

if change code of controller use "mysqlcontext.cs" class directly save info exception "catched":

    [httppost]     public async task<actionresult> upload(invoice invoice)     {         try         {             // "catched" ¿?             this.db.invoices.add(obj);             await this.db.savechangesasync();         }         catch (exception ex)         {             modelstate.addmodelerror("", ex.tostring());         }          return view();     } 

why happening? need able catch exception "insert" or other function in "invoicerepository" class throw inside controller.

any appreciated.

you not awaiting insert method it's not possible action catch exception. visual studio should have given warning message nothing has awaited in method. change code this:

await this.invoices.insert(invoice); 

also, repository should not use async void, should return task


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -