This question is more of a what is the RIGHT way to do something...
The question...is there a proper nesting order between a using block and a try/catch?
Is it ok to nest the entire using statement inside of a try/catch and maintain the benefits of a using block? (or will an exception cause the closing portion of the using statement to get thrown out the window)
Or should you nest the try/catch inside the using statements and only surround the statements that do database access?
Is...
try {
     using( tsmtcowebEntities db = new tsmtcowebEntities() ) {
          violationList = ( from a in db.DriverTrafficViolationDetails
                            where a.DriverTrafficViolation.DriverApplicationId == DriverAppId
                            orderby a.DateOfOccurance descending
                            select a ).ToList<DriverTrafficViolationDetail>();
          GeneralViolation = ( from a in db.DriverTrafficViolations
                               where a.DriverApplicationId == DriverAppId
                               select a ).FirstOrDefault();
     }
} catch { }
less/more correct than...
using( tsmtcowebEntities db = new tsmtcowebEntities() ) {
     try {
          violationList = ( from a in db.DriverTrafficViolationDetails
                            where a.DriverTrafficViolation.DriverApplicationId == DriverAppId
                            orderby a.DateOfOccurance descending
                            select a ).ToList<DriverTrafficViolationDetail>();
          GeneralViolation = ( from a in db.DriverTrafficViolations
                               where a.DriverApplicationId == DriverAppId
                               select a ).FirstOrDefault();
     } catch { }
}
                verb (used with object), used, us·ing. to employ for some purpose; put into service; make use of: to use a knife. to avail oneself of; apply to one's own purposes: to use the facilities. to expend or consume in use: We have used the money provided.
exploiter Add to list Share. An exploiter is a user, someone who takes advantage of other people or things for their own gain. Being an exploiter is selfish and unethical. To exploit someone is to use them in a way that's wrong, like an employer who pays low wages but demands long hours.
The words employ and use are common synonyms of utilize.
The later is better: it will avoid masking exceptions eventually thrown dy dispose. See this article.
It's really a matter of style and how narrow you want to keep the scope of db:
If the using is inside the try/catch block the db variable will only be accessible within the try portion.
If the using is outside the try/catch block it will be visible within the catch portion.
Regardless, the variable will be disposed of correctly because the using block is the equivalent of a try/finally.
Personally I would be wondering why you need to catch exceptions there at all and what, if anything, you are able to do with them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With