Catching an Exception and rethrowing in C# - can it keep its type? -


we have business-critical bit of legacy code, , methods relatively large , code mess. without test framework , being critical our debugging abilities limited. logging messages not useful - "object not set instance of object"... somewhere in 500 lines of code call other methods. because method body 1 big try/catch specific exceptions being caught.

i want propose address better error logging want minimise impact on method. need carry on normal, because business critical. such, people scared touch need check understanding before put idea forward. say, cannot make change , test myself without writing test harness. don't have time due firefighting, , being last thing on friday.

i think way insert smaller try/catch blocks inside method body catch exceptions, log useful information, rethrow specific catch handlers below.

public int somefunc() {     try     {         // many lines of code          // want wrap call gives problems         try         {             brittlefunc(arg1, arg2, ..., arg15);         }         catch (exception e)         {             // log params repro in unit test , rethrow             log(args);             throw;         }          // many more lines of code      }     catch(customexception1 e)     {         //     }     // more...     catch(customexception9 e)     {         //     }     return someint; } 

in inner try/catch write specific catch handlers match @ bottom, messy.

if catch customexception2 inside catch(exception e) , rethrow, end in catch(customexception2 e) ?

yes can that. inside first catch, exception treaded base exception when rethrow next catch catch it's real type.

when rethrow exception please use throw; not thrown ex; see here why


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -