Ignore a specific exception

3

In my try-catch , I want to write the exception that occurred within an object (for historical purposes, necessary for the business rule, since this section occurs in a processing via integration with external sources). >

To do this, I need to make sure in the% w_that the exception was not the database or my ORM, because if I did not give an exception within the exception ... it would be an ugly crash and pimba, system out of thin air.

So, it would look something like this:

Try {
    //Cógido
}
catch (exception ex)
{
     //VERIFICAR SE NÃO É UMA EXCEÇÃO DE BD (MEU CASO ENTITY FRAMEWORK)
}

Is there a possibility? How could I do this in a more generic way?

    
asked by anonymous 17.01.2017 / 13:37

2 answers

5

You are wanting to ignore the most important exception. There are a lot of exceptions that can occur in this case. Good log systems handle issues that can occur on their own so think about using one of them a>.

If you really want to do this you can filter the exceptions:

catch (Exception ex) when (!(ex is DbException) && !(ex is EntityException)) {
    //faz o que deseja aqui
}

But perhaps the most correct is to handle these exceptions before:

catch (ex DbException)) {
    //faz o que deseja aqui
} catch (ex EntityException) {
    //faz o que deseja aqui
} catch (Exception ex) {
    //faz o que deseja aqui
}

I hope you have no other problems. Rare programmers who use exceptions in the right way .

I placed it on GitHub for future reference .

    
17.01.2017 / 13:51
5

Only you know what kind of exception you want to ignore.

If you want to ignore a DbException , do the following

try 
{ 
}
catch (DbException ex)
{
    //ignorar
}
    
17.01.2017 / 13:50