घोंसला बनाने से यह सवाल एक की अधिक है कि क्या कुछका उपयोग कर और कोशिश/पकड़
सवाल करने के लिए ... सही तरीका है ... वहाँ एक using
ब्लॉक और एक try/catch
के बीच एक उचित घोंसले आदेश है?
using
कथन try/catch
के अंदर कथन को ठीक करना और using
ब्लॉक के लाभों को बनाए रखना ठीक है? (या अपवाद का कारण होगा खिड़की से बाहर फेंक दिया कथन का उपयोग के समापन हिस्से को मिलता है)
या आप घोंसला using
बयान अंदर try/catch
और केवल बयान है कि डेटाबेस का उपयोग कर चारों ओर जाना चाहिए?
है ...
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 { }
कम/अधिक से अधिक सही ...
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 { }
}
क्या आपको वास्तव में प्रदान किए गए कोड में अपने आवेदन के सभी विवरणों की आवश्यकता है? मुझे लगता है कि एक सरल बनाया उदाहरण पर्याप्त होगा। –
खाली पकड़ ब्लॉक एक बेहद खराब प्रोग्रामिंग अभ्यास है। ऐसा मत करो! आप बाद में खेद करेंगे। – phoog
@ जोनाथन रेनहार्ट एक उदाहरण बनाने के लिए कोड के ब्लॉक को कॉपी और पेस्ट करना मेरे लिए आसान था। मैं कोड बना देता हूं जब मुझे लगता है कि दुनिया के लिए कोड डालना बुरा हो सकता है। इस मामले में मुझे कोई नकारात्मक नहीं दिखता है तो क्यों नहीं? – Jared