Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using & try/catch nesting

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 { }
}
like image 434
Jared Avatar asked Mar 08 '12 05:03

Jared


People also ask

What is the verb of using?

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.

What is the word for using someone?

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.

What is the word for using what you have?

The words employ and use are common synonyms of utilize.


2 Answers

The later is better: it will avoid masking exceptions eventually thrown dy dispose. See this article.

like image 168
J.N. Avatar answered Nov 15 '22 19:11

J.N.


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.

like image 24
Andrew Kennan Avatar answered Nov 15 '22 17:11

Andrew Kennan