Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, Catch Exception

Tags:

c#

exception

I get a Exception type using

catch(Exception e){
  log.Error(e.GetType()); // it write 'System.Data.EntityException'
}

so I change my code to catch that exception,

try{
...
}catch(EntityException a){
  // need to do something
  log.Error("I got it!");
}catch(Exception e){
  log.Error("No");
}

and the code write only "No".

How can I catch the EntityException before reach Exception?

Thanks

like image 239
Expert wanna be Avatar asked Oct 07 '22 14:10

Expert wanna be


1 Answers

The code you have should work correctly, provided there isn't another EntityException type defined within the current set of using statements for that file or namespace.

Try fully qualifying the type, as in the following:

try{
...
}catch(System.Data.EntityException a){
  // need to do something
  log.Error("I got it!");
}catch(Exception e){
  log.Error("No");
}
like image 127
Reed Copsey Avatar answered Oct 10 '22 03:10

Reed Copsey