c# - How to catch and log exception inside Using statement without using try-catch? -


as know using statement has built in implementation of dispose() , try-catch. want know few things

  1. is possible log exception inside using statement without using try-catch block , either inside or outside statement. if not, why built in statement.

  2. nested or overuse of try-catch not preferred, why such model preferred use.

            using (some_resource)         {             try             {             }             catch             {             }                         {                 //my exception logging mechanism             }         } 

will become

            try             {                 try                 {                 }                 catch                 {                 }                                 {                     //my exception logging mechanism                 }             }             catch             {             }                         {                 //some_resource.dispose()             } 

a using statement involves try/finally; there no catch. frankly, concern overkill; multiply-nested , complex try/catch/finally "undesirable" because:

  • it makes code hard read
  • and harder right (most people wrong, alas)
  • it misused
  • it suggests method doing much

with using, isn't issue; makes intent clean, without adding complexity or concern.

i use:

using (some_resource) {     try {         // code     } catch (exception ex) {         logit(ex);         throw;     } } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -