TypeScript में त्रुटि प्रबंधन: try/catch से जटिल परिदृश्यों तक
\n\nTypeScript, JavaScript पर एक ओवरले होने के नाते, त्रुटि प्रबंधन तंत्र को विरासत में लेता है और उसका विस्तार करता है। उचित अपवाद प्रबंधन एक स्थिर और पूर्वानुमानित एप्लिकेशन की कुंजी है। इस लेख में, हम त्रुटियों के साथ काम करने के सभी पहलुओं को कवर करेंगे: बुनियादी try/catch से लेकर अपनी स्वयं की टाइप की गई त्रुटि क्लासेस बनाने और उन्नत रणनीतियों तक।
मूल सिंटैक्स: try, catch, finally
\n\nTypeScript (और JavaScript) में त्रुटि प्रबंधन का मुख्य तंत्र try...catch...finally कंस्ट्रक्शन है। यह कोड में उत्पन्न होने वाले अपवादों को इंटरसेप्ट करने और उन पर प्रतिक्रिया करने की अनुमति देता है।
function parseJSON(data: string): object {\n try {\n return JSON.parse(data);\n } catch (error) {\n console.error('JSON पार्सिंग त्रुटि:', error);\n return {};\n }\n}\n\nparseJSON('{"name": "TypeScript"}'); // { name: 'TypeScript' }\nparseJSON('अमान्य JSON'); // {} (त्रुटि पकड़ी गई)\n\nfinally ब्लॉक हमेशा निष्पादित होता है - try के सफल समापन के बाद और त्रुटि पकड़े जाने के बाद भी। यह संसाधनों को साफ करने (फ़ाइलें बंद करना, कनेक्शन) के लिए एक आदर्श स्थान है।
function processData(data: string): void {\n let resource: any = null;\n try {\n resource = acquireResource();\n // ... खतरनाक कोड\n } catch (error) {\n console.error('प्रसंस्करण त्रुटि:', error);\n } finally {\n if (resource) {\n resource.release(); // गारंटीकृत सफाई\n }\n }\n}\n\nत्रुटियों का टाइपीकरण: unknown समस्या
\n\nTypeScript की मुख्य विशेषता - catch ब्लॉक में, error वेरिएबल का प्रकार डिफ़ॉल्ट रूप से unknown होता है (TS 4.0 से शुरू)। यह जानबूझकर किया गया है ताकि डेवलपर को त्रुटि के प्रकार की स्पष्ट रूप से जांच करने के लिए मजबूर किया जा सके, न कि यह मान लिया जाए कि यह Error है।
try {\n // कुछ कोड\n} catch (error) {\n // error का प्रकार unknown है\n // त्रुटि: 'unknown' प्रकार पर 'message' गुण मौजूद नहीं है\n // console.log(error.message);\n\n // सही तरीका - प्रकार की जांच\n if (error instanceof Error) {\n console.log('संदेश:', error.message);\n } else {\n console.log('अज्ञात त्रुटि:', error);\n }\n}\n\nयह क्यों महत्वपूर्ण है? JavaScript में, catch ब्लॉक में कुछ भी आ सकता है: स्ट्रिंग, संख्या, ऑब्जेक्ट, null या undefined भी। अनिवार्य प्रकार की जांच रनटाइम के दौरान त्रुटियों से बचाती है।
कस्टम त्रुटि क्लासेस बनाना
\n\nजटिल एप्लिकेशन के लिए, अक्सर त्रुटियों के प्रकारों में अंतर करना आवश्यक होता है। TypeScript में, बिल्ट-इन Error का विस्तार करते हुए अपनी स्वयं की त्रुटि क्लासेस का पदानुक्रम बनाना आसान है।
// एप्लिकेशन त्रुटियों के लिए बेस क्लास\nclass AppError extends Error {\n public readonly statusCode: number;\n public readonly isOperational: boolean;\n\n constructor(message: string, statusCode: number = 500, isOperational: boolean = true) {\n super(message);\n this.name = this.constructor.name;\n this.statusCode = statusCode;\n this.isOperational = isOperational;\n\n // प्रोटोटाइप को पुनर्स्थापित करें (instanceof के लिए महत्वपूर्ण)\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n// विशिष्ट त्रुटि प्रकार\nclass ValidationError extends AppError {\n public readonly field: string;\n\n constructor(message: string, field: string) {\n super(message, 400);\n this.field = field;\n }\n}\n\nclass NotFoundError extends AppError {\n constructor(message: string = 'संसाधन नहीं मिला') {\n super(message, 404);\n }\n}\n\n// उपयोग\nfunction getUser(id: number): User {\n if (id <= 0) {\n throw new ValidationError('ID एक धनात्मक संख्या होनी चाहिए', 'id');\n }\n // ... उपयोगकर्ता खोजें\n throw new NotFoundError