{"id":237,"date":"2026-03-09T22:25:51","date_gmt":"2026-03-09T14:25:51","guid":{"rendered":"https:\/\/www.resilence.cn\/?p=237"},"modified":"2026-03-09T22:25:51","modified_gmt":"2026-03-09T14:25:51","slug":"day-9-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.resilence.cn\/?p=237","title":{"rendered":"Day-9-\u9519\u8bef\u5904\u7406"},"content":{"rendered":"<h1>Day 9: \u9519\u8bef\u5904\u7406<\/h1>\n<h2>\ud83c\udfaf \u5b66\u4e60\u76ee\u6807<\/h2>\n<ul>\n<li>\u7406\u89e3 \u9519\u8bef\u5904\u7406 \u7684\u6838\u5fc3\u6982\u5ff5\u548c\u5de5\u4f5c\u539f\u7406<\/li>\n<li>\u638c\u63e1\u76f8\u5173\u8bed\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5<\/li>\n<li>\u5b66\u4f1a\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528 \u9519\u8bef\u5904\u7406<\/li>\n<\/ul>\n<h2>\ud83d\udca1 \u6838\u5fc3\u6982\u5ff5<\/h2>\n<p>\u9519\u8bef\u5904\u7406\u662f\u7f16\u5199\u5065\u58ee\u7a0b\u5e8f\u7684\u5173\u952e\u3002JavaScript\u63d0\u4f9b\u4e86\u591a\u79cd\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u5305\u62ectry-catch\u3001throw\u3001Error\u5bf9\u8c61\u7b49\uff0c\u5e2e\u52a9\u6211\u4eec\u4f18\u96c5\u5730\u5904\u7406\u548c\u8c03\u8bd5\u9519\u8bef\u3002<\/p>\n<h3>\u5173\u952e\u6982\u5ff5<\/h3>\n<h4>1. \u9519\u8bef\u7c7b\u578b<\/h4>\n<p>JavaScript\u6709\u4ee5\u4e0b\u5e38\u89c1\u9519\u8bef\u7c7b\u578b\uff1a<\/p>\n<ol>\n<li><strong>Error<\/strong>\uff1a\u57fa\u7840\u9519\u8bef\u7c7b\u578b<\/li>\n<li><strong>SyntaxError<\/strong>\uff1a\u8bed\u6cd5\u9519\u8bef<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">eval('1 + * 2');  \/\/ SyntaxError\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>ReferenceError<\/strong>\uff1a\u5f15\u7528\u4e0d\u5b58\u5728\u7684\u53d8\u91cf<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">console.log(notDefined);  \/\/ ReferenceError\n<\/code><\/pre>\n<ol start=\"4\">\n<li><strong>TypeError<\/strong>\uff1a\u7c7b\u578b\u9519\u8bef<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">null.toString();  \/\/ TypeError\n<\/code><\/pre>\n<ol start=\"5\">\n<li><strong>RangeError<\/strong>\uff1a\u503c\u8d85\u51fa\u6709\u6548\u8303\u56f4<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">new Array(-1);  \/\/ RangeError\n<\/code><\/pre>\n<ol start=\"6\">\n<li><strong>URIError<\/strong>\uff1aURI\u76f8\u5173\u9519\u8bef<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">decodeURIComponent('%');  \/\/ URIError\n<\/code><\/pre>\n<h4>2. try-catch-finally<\/h4>\n<p>JavaScript\u7684\u9519\u8bef\u5904\u7406\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"language-javascript\">try {\n    \/\/ \u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801\n    riskyOperation();\n} catch (error) {\n    \/\/ \u6355\u83b7\u9519\u8bef\u540e\u7684\u5904\u7406\n    console.error('\u53d1\u751f\u9519\u8bef:', error.message);\n} finally {\n    \/\/ \u65e0\u8bba\u662f\u5426\u51fa\u9519\u90fd\u4f1a\u6267\u884c\n    cleanup();\n}\n<\/code><\/pre>\n<p><strong>\u91cd\u8981\u8bf4\u660e<\/strong>\uff1a<\/p>\n<ul>\n<li>catch\u5757\u53ef\u4ee5\u7701\u7565\uff08\u914d\u5408finally\u4f7f\u7528\uff09<\/li>\n<li>finally\u5757\u603b\u662f\u6267\u884c\uff08\u5373\u4f7fcatch\u4e2d\u6709return\uff09<\/li>\n<li>try\u5757\u4e2d\u629b\u51fa\u7684\u9519\u8bef\u4f1a\u88abcatch\u6355\u83b7<\/li>\n<\/ul>\n<h4>3. throw\u8bed\u53e5<\/h4>\n<p>\u4e3b\u52a8\u629b\u51fa\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u629b\u51faError\u5bf9\u8c61\nthrow new Error('\u81ea\u5b9a\u4e49\u9519\u8bef');\n\n\/\/ \u629b\u51fa\u7279\u5b9a\u7c7b\u578b\u9519\u8bef\nthrow new TypeError('\u7c7b\u578b\u9519\u8bef');\nthrow new ReferenceError('\u5f15\u7528\u9519\u8bef');\n\n\/\/ \u629b\u51fa\u4efb\u610f\u503c\uff08\u4e0d\u63a8\u8350\uff09\nthrow '\u7b80\u5355\u9519\u8bef\u5b57\u7b26\u4e32';\nthrow 404;\nthrow {code: 500, message: '\u670d\u52a1\u5668\u9519\u8bef'};\n<\/code><\/pre>\n<p><strong>\u81ea\u5b9a\u4e49\u9519\u8bef<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">class CustomError extends Error {\n    constructor(message) {\n        super(message);\n        this.name = 'CustomError';\n    }\n}\n\nthrow new CustomError('\u81ea\u5b9a\u4e49\u9519\u8bef');\n<\/code><\/pre>\n<h2>\ud83d\udcdd \u6838\u5fc3API\u548c\u8bed\u6cd5<\/h2>\n<h3>Error\u5bf9\u8c61<\/h3>\n<pre><code class=\"language-javascript\">new Error([message[, fileName[, lineNumber]]])\n<\/code><\/pre>\n<p><strong>\u529f\u80fd<\/strong>: \u521b\u5efa\u9519\u8bef\u5bf9\u8c61<\/p>\n<p><strong>\u53c2\u6570<\/strong>:<\/p>\n<ul>\n<li>message: \u9519\u8bef\u63cf\u8ff0\u4fe1\u606f<\/li>\n<li>fileName: \u6587\u4ef6\u540d\uff08\u975e\u6807\u51c6\uff09<\/li>\n<li>lineNumber: \u884c\u53f7\uff08\u975e\u6807\u51c6\uff09<\/li>\n<\/ul>\n<p><strong>\u5c5e\u6027<\/strong>:<\/p>\n<ul>\n<li>message: \u9519\u8bef\u4fe1\u606f<\/li>\n<li>name: \u9519\u8bef\u540d\u79f0<\/li>\n<li>stack: \u9519\u8bef\u5806\u6808\uff08\u975e\u6807\u51c6\u4f46\u5e7f\u6cdb\u652f\u6301\uff09<\/li>\n<\/ul>\n<h2>\ud83c\udfae \u793a\u4f8b\u4ee3\u7801<\/h2>\n<h3>\u793a\u4f8b1\uff1a\u57fa\u672c\u9519\u8bef\u5904\u7406<\/h3>\n<pre><code class=\"language-javascript\">function divide(a, b) {\n    try {\n        if (typeof a !== 'number' || typeof b !== 'number') {\n            throw new TypeError('\u53c2\u6570\u5fc5\u987b\u662f\u6570\u5b57');\n        }\n\n        if (b === 0) {\n            throw new Error('\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6');\n        }\n\n        return a \/ b;\n\n    } catch (error) {\n        console.error('\u9519\u8bef\u7c7b\u578b:', error.name);\n        console.error('\u9519\u8bef\u4fe1\u606f:', error.message);\n        console.error('\u9519\u8bef\u5806\u6808:', error.stack);\n\n        return null;  \/\/ \u8fd4\u56de\u9ed8\u8ba4\u503c\n\n    } finally {\n        console.log('\u9664\u6cd5\u64cd\u4f5c\u5b8c\u6210');\n    }\n}\n\nconsole.log(divide(10, 2));    \/\/ 5\nconsole.log(divide(10, 0));    \/\/ null\uff08\u6355\u83b7\u9519\u8bef\uff09\nconsole.log(divide('10', 2));  \/\/ null\uff08\u6355\u83b7\u9519\u8bef\uff09\n\n\/*\n\u8f93\u51fa\uff1a\n\u9664\u6cd5\u64cd\u4f5c\u5b8c\u6210\n5\n\u9519\u8bef\u7c7b\u578b: Error\n\u9519\u8bef\u4fe1\u606f: \u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\n\u9664\u6cd5\u64cd\u4f5c\u5b8c\u6210\nnull\n\u9519\u8bef\u7c7b\u578b: TypeError\n\u9519\u8bef\u4fe1\u606f: \u53c2\u6570\u5fc5\u987b\u662f\u6570\u5b57\n\u9664\u6cd5\u64cd\u4f5c\u5b8c\u6210\nnull\n*\/\n\n<\/code><\/pre>\n<h3>\u793a\u4f8b2\uff1a\u5f02\u6b65\u9519\u8bef\u5904\u7406<\/h3>\n<pre><code class=\"language-javascript\">\/\/ Promise\u9519\u8bef\u5904\u7406\nfetchData()\n    .then(data =&gt; {\n        console.log('\u6570\u636e:', data);\n    })\n    .catch(error =&gt; {\n        console.error('\u8bf7\u6c42\u5931\u8d25:', error);\n    });\n\n\/\/ async\/await\u9519\u8bef\u5904\u7406\nasync function getData() {\n    try {\n        const response = await fetch('\/api\/data');\n        const data = await response.json();\n        return data;\n    } catch (error) {\n        console.error('\u83b7\u53d6\u6570\u636e\u5931\u8d25:', error);\n        \/\/ \u53ef\u4ee5\u91cd\u65b0\u629b\u51fa\u6216\u8fd4\u56de\u9ed8\u8ba4\u503c\n        return null;\n    }\n}\n\n\/\/ \u4e8b\u4ef6\u9519\u8bef\u5904\u7406\nelement.addEventListener('click', async function() {\n    try {\n        await riskyOperation();\n    } catch (error) {\n        console.error('\u64cd\u4f5c\u5931\u8d25:', error);\n        \/\/ \u663e\u793a\u9519\u8bef\u63d0\u793a\u7ed9\u7528\u6237\n        showErrorToast(error.message);\n    }\n});\n\n<\/code><\/pre>\n<h3>\u793a\u4f8b3\uff1a\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b<\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u9519\u8bef\nclass ValidationError extends Error {\n    constructor(field, message) {\n        super(message);\n        this.name = 'ValidationError';\n        this.field = field;\n    }\n}\n\n\/\/ \u81ea\u5b9a\u4e49API\u9519\u8bef\nclass APIError extends Error {\n    constructor(statusCode, message) {\n        super(message);\n        this.name = 'APIError';\n        this.statusCode = statusCode;\n    }\n}\n\n\/\/ \u4f7f\u7528\u81ea\u5b9a\u4e49\u9519\u8bef\nfunction validateEmail(email) {\n    if (!email || !email.includes('@')) {\n        throw new ValidationError('email', '\u65e0\u6548\u7684\u90ae\u7bb1\u5730\u5740');\n    }\n}\n\nfunction fetchUserData(userId) {\n    if (!userId) {\n        throw new APIError(400, '\u7f3a\u5c11\u7528\u6237ID');\n    }\n\n    \/\/ \u6a21\u62dfAPI\u8c03\u7528\n    if (userId === '404') {\n        throw new APIError(404, '\u7528\u6237\u4e0d\u5b58\u5728');\n    }\n\n    return {id: userId, name: '\u5f20\u4e09'};\n}\n\n\/\/ \u4f7f\u7528\ntry {\n    validateEmail('invalid-email');\n} catch (error) {\n    if (error instanceof ValidationError) {\n        console.error(`\u5b57\u6bb5 ${error.field} \u9a8c\u8bc1\u5931\u8d25: ${error.message}`);\n    }\n}\n\ntry {\n    const user = fetchUserData('404');\n} catch (error) {\n    if (error instanceof APIError) {\n        console.error(`API\u9519\u8bef ${error.statusCode}: ${error.message}`);\n        \/\/ \u6839\u636e\u72b6\u6001\u7801\u5904\u7406\n        if (error.statusCode === 404) {\n            showUserNotFoundPage();\n        }\n    }\n}\n\n<\/code><\/pre>\n<h3>\u793a\u4f8b4\uff1a\u9519\u8bef\u8fb9\u754c\uff08Error Boundary\uff09<\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u9519\u8bef\u5904\u7406\u5305\u88c5\u5668\nfunction safeExecute(fn, errorHandler) {\n    return function(...args) {\n        try {\n            return fn.apply(this, args);\n        } catch (error) {\n            console.error('\u6267\u884c\u51fa\u9519:', error);\n            if (errorHandler) {\n                return errorHandler(error);\n            }\n        }\n    };\n}\n\n\/\/ \u4f7f\u7528\nfunction riskyOperation(x, y) {\n    if (y === 0) throw new Error('\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6');\n    return x \/ y;\n}\n\nconst safeDivide = safeExecute(riskyOperation, function(error) {\n    console.error('\u9664\u6cd5\u5931\u8d25:', error.message);\n    return 0;  \/\/ \u9ed8\u8ba4\u503c\n});\n\nconsole.log(safeDivide(10, 2));  \/\/ 5\nconsole.log(safeDivide(10, 0));  \/\/ 0\uff08\u9519\u8bef\u88ab\u6355\u83b7\uff09\n\n\/\/ \u5f02\u6b65\u7248\u672c\nasync function safeAsync(fn, errorHandler) {\n    return async function(...args) {\n        try {\n            return await fn.apply(this, args);\n        } catch (error) {\n            console.error('\u5f02\u6b65\u64cd\u4f5c\u51fa\u9519:', error);\n            if (errorHandler) {\n                return errorHandler(error);\n            }\n        }\n    };\n}\n\nconst safeFetch = safeAsync(fetchData, function(error) {\n    console.error('\u83b7\u53d6\u6570\u636e\u5931\u8d25');\n    return [];  \/\/ \u8fd4\u56de\u7a7a\u6570\u7ec4\n});\n\nconst data = await safeFetch();\n\n<\/code><\/pre>\n<h2>\u270d\ufe0f \u7ec3\u4e60\u4efb\u52a1<\/h2>\n<h3>\u7ec3\u4e60 1: \u7ec3\u4e601\uff1a\u5b9e\u73b0\u8f93\u5165\u9a8c\u8bc1<\/h3>\n<p>\u521b\u5efa\u4e00\u4e2a\u8868\u5355\u9a8c\u8bc1\u51fd\u6570\uff0c\u8981\u6c42\uff1a<\/p>\n<ol>\n<li>\u9a8c\u8bc1\u7528\u6237\u5bf9\u8c61<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">function validateUser(user) {\n    \/\/ \u9a8c\u8bc1\u89c4\u5219\uff1a\n    \/\/ - name: \u5fc5\u586b\uff0c2-20\u5b57\u7b26\n    \/\/ - email: \u5fc5\u586b\uff0c\u6709\u6548\u90ae\u7bb1\u683c\u5f0f\n    \/\/ - age: \u53ef\u9009\uff0c18-120\u4e4b\u95f4\n    \/\/ - \u8fd4\u56de\uff1a{valid: boolean, errors: string[]}\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\n<p>\u629b\u51faValidationError<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528try-catch\u5904\u7406<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u6d4b\u8bd5\u7528\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">try {\n    validateUser({name: 'A', email: 'invalid'});\n} catch (error) {\n    console.log(error.errors);\n    \/\/ ['name\u957f\u5ea6\u5fc5\u987b2-20\u5b57\u7b26', '\u90ae\u7bb1\u683c\u5f0f\u65e0\u6548']\n}\n<\/code><\/pre>\n<p><strong>\u63d0\u793a<\/strong>: \u68c0\u67e5\u6bcf\u4e2a\u5b57\u6bb5\uff0c\u6536\u96c6\u6240\u6709\u9519\u8bef\u540e\u4e00\u8d77\u629b\u51fa<\/p>\n<h3>\u7ec3\u4e60 2: \u7ec3\u4e602\uff1a\u91cd\u8bd5\u673a\u5236<\/h3>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u5e26\u91cd\u8bd5\u7684\u5f02\u6b65\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-javascript\">async function retry(fn, maxRetries = 3, delay = 1000) {\n    \/\/ \u5b9e\u73b0\u91cd\u8bd5\u903b\u8f91\uff1a\n    \/\/ 1. \u5c1d\u8bd5\u6267\u884cfn()\n    \/\/ 2. \u5931\u8d25\u540e\u7b49\u5f85delay\u6beb\u79d2\n    \/\/ 3. \u91cd\u8bd5\uff0c\u6700\u591amaxRetries\u6b21\n    \/\/ 4. \u5168\u90e8\u5931\u8d25\u540e\u629b\u51fa\u6700\u540e\u4e00\u4e2a\u9519\u8bef\n}\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">let attempts = 0;\nasync function unstableFunction() {\n    attempts++;\n    if (attempts &lt; 3) {\n        throw new Error('\u6682\u65f6\u5931\u8d25');\n    }\n    return '\u6210\u529f';\n}\n\nconst result = await retry(unstableFunction, 5, 500);\nconsole.log(result);  \/\/ '\u6210\u529f'\uff08\u91cd\u8bd52\u6b21\u540e\u6210\u529f\uff09\n<\/code><\/pre>\n<p><strong>\u63d0\u793a<\/strong>: \u4f7f\u7528for\u5faa\u73af\u6216\u9012\u5f52\uff0c\u914d\u5408await sleep()<\/p>\n<h3>\u7ec3\u4e60 3: \u7ec3\u4e603\uff1a\u9519\u8bef\u65e5\u5fd7\u7cfb\u7edf<\/h3>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u9519\u8bef\u65e5\u5fd7\u7cfb\u7edf\uff1a<\/p>\n<pre><code class=\"language-javascript\">class ErrorLogger {\n    \/\/ \u8bb0\u5f55\u9519\u8bef\n    log(error, context = {}) {}\n\n    \/\/ \u83b7\u53d6\u6240\u6709\u9519\u8bef\n    getErrors() {}\n\n    \/\/ \u6309\u7c7b\u578b\u7b5b\u9009\n    getErrorsByType(type) {}\n\n    \/\/ \u6e05\u7a7a\u65e5\u5fd7\n    clear() {}\n\n    \/\/ \u5bfc\u51fa\u4e3aJSON\n    export() {}\n}\n<\/code><\/pre>\n<p><strong>\u529f\u80fd\u8981\u6c42<\/strong>\uff1a<\/p>\n<ul>\n<li>\u8bb0\u5f55\u9519\u8bef\u65f6\u95f4\u3001\u7c7b\u578b\u3001\u6d88\u606f\u3001\u5806\u6808<\/li>\n<li>\u652f\u6301\u6dfb\u52a0\u4e0a\u4e0b\u6587\u4fe1\u606f\uff08\u7528\u6237\u3001\u64cd\u4f5c\u7b49\uff09<\/li>\n<li>\u7edf\u8ba1\u5404\u7c7b\u9519\u8bef\u6570\u91cf<\/li>\n<li>\u5bfc\u51fa\u683c\u5f0f\u5316\u7684\u9519\u8bef\u62a5\u544a<\/li>\n<\/ul>\n<p><strong>\u6d4b\u8bd5<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">const logger = new ErrorLogger();\n\ntry {\n    riskyOperation();\n} catch (error) {\n    logger.log(error, {user: '\u5f20\u4e09', action: '\u4fdd\u5b58\u6570\u636e'});\n}\n\nconsole.log(logger.export());\n<\/code><\/pre>\n<p><strong>\u63d0\u793a<\/strong>: \u4f7f\u7528\u6570\u7ec4\u5b58\u50a8\u9519\u8bef\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u9519\u8bef\u5305\u542btimestamp\u3001error\u3001context<\/p>\n<h2>\ud83c\udf93 \u4eca\u65e5\u6311\u6218<\/h2>\n<h3>\u4eca\u65e5\u6311\u6218\uff1a\u5b9e\u73b0\u5b8c\u6574\u7684API\u5ba2\u6237\u7aef<\/h3>\n<p>\u521b\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u7684API\u5ba2\u6237\u7aef\u7c7b\uff1a<\/p>\n<p><strong>\u529f\u80fd\u8981\u6c42<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u8bf7\u6c42\u62e6\u622a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">class APIClient {\n    constructor(baseURL) {\n        this.baseURL = baseURL;\n        this.interceptors = [];\n    }\n\n    \/\/ \u6dfb\u52a0\u62e6\u622a\u5668\n    useInterceptor(fn) {\n        this.interceptors.push(fn);\n    }\n\n    async request(url, options = {}) {\n        \/\/ \u6267\u884c\u6240\u6709\u62e6\u622a\u5668\n        \/\/ \u53d1\u9001\u8bf7\u6c42\n        \/\/ \u5904\u7406\u54cd\u5e94\u548c\u9519\u8bef\n    }\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong><\/li>\n<\/ol>\n<ul>\n<li>\u7f51\u7edc\u9519\u8bef\uff1a\u91cd\u8bd53\u6b21<\/li>\n<li>4xx\u9519\u8bef\uff1a\u629b\u51faClientError<\/li>\n<li>5xx\u9519\u8bef\uff1a\u629b\u51faServerError<\/li>\n<li>\u8d85\u65f6\u9519\u8bef\uff1a\u629b\u51faTimeoutError<\/li>\n<\/ul>\n<ol start=\"3\">\n<li><strong>\u54cd\u5e94\u62e6\u622a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>\u81ea\u52a8\u89e3\u6790JSON<\/li>\n<li>\u68c0\u67e5\u54cd\u5e94\u72b6\u6001<\/li>\n<li>\u63d0\u53d6\u6570\u636e<\/li>\n<\/ul>\n<ol start=\"4\">\n<li><strong>\u4fbf\u6377\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">client.get(url, params)\nclient.post(url, data)\nclient.put(url, data)\nclient.delete(url)\n<\/code><\/pre>\n<ol start=\"5\">\n<li><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">const api = new APIClient('https:\/\/api.example.com');\n\n\/\/ \u6dfb\u52a0\u8ba4\u8bc1\u62e6\u622a\u5668\napi.useInterceptor((options) =&gt; {\n    options.headers = {\n        ...options.headers,\n        'Authorization': `Bearer ${getToken()}`\n    };\n    return options;\n});\n\n\/\/ \u4f7f\u7528\ntry {\n    const user = await api.get('\/user\/123');\n    console.log(user);\n} catch (error) {\n    if (error instanceof ClientError) {\n        console.error('\u5ba2\u6237\u7aef\u9519\u8bef:', error.message);\n    } else if (error instanceof ServerError) {\n        console.error('\u670d\u52a1\u5668\u9519\u8bef:', error.message);\n    }\n}\n<\/code><\/pre>\n<p><strong>\u6269\u5c55\u529f\u80fd<\/strong>\uff08\u53ef\u9009\uff09\uff1a<\/p>\n<ul>\n<li>\u8bf7\u6c42\u7f13\u5b58<\/li>\n<li>\u8bf7\u6c42\u53d6\u6d88<\/li>\n<li>\u4e0a\u4f20\/\u4e0b\u8f7d\u8fdb\u5ea6<\/li>\n<li>\u8bf7\u6c42\u961f\u5217\uff08\u5e76\u53d1\u9650\u5236\uff09<\/li>\n<\/ul>\n<p><strong>\u63d0\u793a<\/strong>: \u4f7f\u7528fetch API\uff0c\u914d\u5408try-catch\u548c\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b<\/p>\n<h2>\ud83d\udca1 \u5e38\u89c1\u95ee\u9898<\/h2>\n<h3>Q1: \u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528try-catch\uff1f<\/h3>\n<p><strong>A<\/strong>: <strong>\u5e94\u8be5\u4f7f\u7528\u7684\u573a\u666f<\/strong>\uff1a<\/p>\n<p><strong>1. \u53ef\u80fd\u5931\u8d25\u7684\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">try {\n    const data = JSON.parse(userInput);\n} catch (e) {\n    console.error('JSON\u89e3\u6790\u5931\u8d25');\n}\n<\/code><\/pre>\n<p><strong>2. \u5916\u90e8\u8d44\u6e90\u8bbf\u95ee<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">try {\n    const response = await fetch(url);\n} catch (e) {\n    console.error('\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25');\n}\n<\/code><\/pre>\n<p><strong>3. \u7c7b\u578b\u8f6c\u6362<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">try {\n    const num = parseInt(userInput);\n} catch (e) {\n    \/\/ \u5904\u7406\u8f6c\u6362\u9519\u8bef\n}\n<\/code><\/pre>\n<p><strong>\u4e0d\u5e94\u8be5\u4f7f\u7528\u7684\u573a\u666f<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u63a7\u5236\u6d41\u7a0b<\/strong>\uff08\u4e0d\u8981\u7528\u9519\u8bef\u5904\u7406\u4ee3\u66ffif\uff09<\/li>\n<li><strong>\u53ef\u9884\u89c1\u7684\u9519\u8bef<\/strong>\uff08\u5e94\u8be5\u7528if\u68c0\u67e5\uff09<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">\/\/ \u274c \u4e0d\u597d\ntry {\n    if (obj.prop) {\n        \/\/ ...\n    }\n} catch (e) {}\n\n\/\/ \u2705 \u597d\nif (obj &amp;&amp; obj.prop) {\n    \/\/ ...\n}\n<\/code><\/pre>\n<h3>Q2: \u5982\u4f55\u8c03\u8bd5\u672a\u6355\u83b7\u7684\u9519\u8bef\uff1f<\/h3>\n<p><strong>A<\/strong>: <strong>\u65b9\u6cd51\uff1a\u5168\u5c40\u9519\u8bef\u5904\u7406<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u6355\u83b7\u672a\u5904\u7406\u7684\u9519\u8bef\nwindow.addEventListener('error', function(event) {\n    console.error('\u5168\u5c40\u9519\u8bef:', event.error);\n    \/\/ \u53d1\u9001\u5230\u65e5\u5fd7\u670d\u52a1\u5668\n    logError(event.error);\n});\n\n\/\/ \u6355\u83b7\u672a\u5904\u7406\u7684Promise\u62d2\u7edd\nwindow.addEventListener('unhandledrejection', function(event) {\n    console.error('\u672a\u5904\u7406\u7684Promise:', event.reason);\n    event.preventDefault();  \/\/ \u963b\u6b62\u9ed8\u8ba4\u5904\u7406\n});\n<\/code><\/pre>\n<p><strong>\u65b9\u6cd52\uff1asource map<\/strong>\uff1a<\/p>\n<ul>\n<li>\u751f\u4ea7\u73af\u5883\u542f\u7528source map<\/li>\n<li>\u9519\u8bef\u5806\u6808\u663e\u793a\u539f\u59cb\u4ee3\u7801\u4f4d\u7f6e<\/li>\n<\/ul>\n<p><strong>\u65b9\u6cd53\uff1a\u9519\u8bef\u76d1\u63a7\u670d\u52a1<\/strong>\uff1a<\/p>\n<ul>\n<li>Sentry<\/li>\n<li>Rollbar<\/li>\n<li>Bugsnag<\/li>\n<\/ul>\n<p><strong>\u65b9\u6cd54\uff1a\u5f00\u53d1\u8005\u5de5\u5177<\/strong>\uff1a<\/p>\n<ul>\n<li>Chrome DevTools\u7684Console\u9762\u677f<\/li>\n<li>Break on exceptions\uff08\u6682\u505c\u5728\u5f02\u5e38\u5904\uff09<\/li>\n<li>Stack trace\u5206\u6790<\/li>\n<\/ul>\n<h3>Q3: Promise\u9519\u8bef\u548ctry-catch\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h3>\n<p><strong>A<\/strong>: <strong>\u5173\u952e\u533a\u522b<\/strong>\uff1a<\/p>\n<p><strong>1. try-catch\u4e0d\u80fd\u6355\u83b7\u5f02\u6b65\u9519\u8bef<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">try {\n    setTimeout(() =&gt; {\n        throw new Error('\u65e0\u6cd5\u6355\u83b7');\n    }, 100);\n} catch (e) {\n    \/\/ \u4e0d\u4f1a\u6267\u884c\n}\n\n\/\/ \u89e3\u51b3\uff1a\u4f7f\u7528Promise\ncatch Promise.reject('\u53ef\u4ee5\u6355\u83b7');\n<\/code><\/pre>\n<p><strong>2. Promise\u9519\u8bef\u7684\u4f20\u64ad<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">Promise.reject('\u9519\u8bef')\n    .then(() =&gt; console.log('\u4e0d\u6267\u884c'))\n    .then(() =&gt; console.log('\u4e0d\u6267\u884c'))\n    .catch(err =&gt; console.log('\u6355\u83b7:', err));\n\/\/ \u9519\u8bef\u4f1a\u4e00\u76f4\u4f20\u64ad\u76f4\u5230\u88ab\u6355\u83b7\n<\/code><\/pre>\n<p><strong>3. async\/await\u7edf\u4e00\u4e86\u9519\u8bef\u5904\u7406<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">async function foo() {\n    try {\n        await Promise.reject('\u9519\u8bef');\n    } catch (e) {\n        console.log('\u6355\u83b7:', e);\n    }\n}\n<\/code><\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u540c\u6b65\u4ee3\u7801\uff1atry-catch<\/li>\n<li>\u5f02\u6b65\u4ee3\u7801\uff1aPromise.catch()\u6216async\/await<\/li>\n<li>\u5168\u5c40\uff1aunhandledrejection\u4e8b\u4ef6<\/li>\n<\/ul>\n<h3>Q4: \u5982\u4f55\u521b\u5efa\u6709\u610f\u4e49\u7684\u9519\u8bef\u4fe1\u606f\uff1f<\/h3>\n<p><strong>A<\/strong>: <strong>\u597d\u7684\u9519\u8bef\u4fe1\u606f\u5e94\u8be5<\/strong>\uff1a<\/p>\n<p><strong>1. \u6e05\u6670\u5177\u4f53<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u274c \u4e0d\u597d\nthrow new Error('\u9519\u8bef');\n\n\/\/ \u2705 \u597d\nthrow new Error('\u7528\u6237ID\u4e0d\u80fd\u4e3a\u7a7a');\n<\/code><\/pre>\n<p><strong>2. \u5305\u542b\u4e0a\u4e0b\u6587<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">function updateUser(userId, data) {\n    if (!userId) {\n        throw new Error(\n            `updateUser\u5931\u8d25: userId\u4e3a\u7a7a (data: ${JSON.stringify(data)})`\n        );\n    }\n}\n<\/code><\/pre>\n<p><strong>3. \u63d0\u4f9b\u89e3\u51b3\u5efa\u8bae<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">class ValidationError extends Error {\n    constructor(field, value, rule) {\n        super(\n            `\u5b57\u6bb5${field}\u9a8c\u8bc1\u5931\u8d25: ${value} \u4e0d\u7b26\u5408\u89c4\u5219${rule}. ` +\n            `\u8bf7\u68c0\u67e5${field}\u7684\u503c\u662f\u5426\u6b63\u786e\u3002`\n        );\n        this.field = field;\n        this.value = value;\n    }\n}\n<\/code><\/pre>\n<p><strong>4. \u4f7f\u7528\u9519\u8bef\u4ee3\u7801<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">const ERROR_CODES = {\n    USER_NOT_FOUND: 'USER_NOT_FOUND',\n    INVALID_PASSWORD: 'INVALID_PASSWORD',\n    NETWORK_ERROR: 'NETWORK_ERROR'\n};\n\nthrow new Error(ERROR_CODES.USER_NOT_FOUND);\n<\/code><\/pre>\n<p><strong>5. \u7ed3\u6784\u5316\u9519\u8bef<\/strong>\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u8fd4\u56de\u9519\u8bef\u5bf9\u8c61\u800c\u4e0d\u662fthrow\nfunction safeOperation() {\n    return {\n        success: false,\n        error: {\n            code: 'INVALID_INPUT',\n            message: '\u8f93\u5165\u65e0\u6548',\n            details: {field: 'email', value: 'bad'}\n        }\n    };\n}\n<\/code><\/pre>\n<hr>\n<p><strong>\u5b66\u4e60\u65f6\u95f4<\/strong>: 2026-03-09 22:22<br \/>\n<strong>\u96be\u5ea6<\/strong>: \u2b50\u2b50\u2b50\u2b50\u2606<br \/>\n<strong>\u9884\u8ba1\u7528\u65f6<\/strong>: 2-3 \u5c0f\u65f6<br \/>\n<strong>\u5173\u952e\u8bcd<\/strong>: \u9519\u8bef\u5904\u7406<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Day 9: \u9519\u8bef\u5904\u7406 \ud83c\udfaf \u5b66\u4e60\u76ee\u6807 \u7406\u89e3 \u9519\u8bef\u5904\u7406 \u7684\u6838\u5fc3\u6982\u5ff5\u548c\u5de5\u4f5c\u539f\u7406 \u638c\u63e1\u76f8\u5173\u8bed\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5 \u5b66\u4f1a\u5728\u5b9e\u9645 &#8230; <a title=\"Day-9-\u9519\u8bef\u5904\u7406\" class=\"read-more\" href=\"https:\/\/www.resilence.cn\/?p=237\" aria-label=\"\u7ee7\u7eed\u9605\u8bfbDay-9-\u9519\u8bef\u5904\u7406\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-237","post","type-post","status-publish","format-standard","hentry","category-studycoding"],"_links":{"self":[{"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/posts\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=237"}],"version-history":[{"count":1,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/posts\/237\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=\/wp\/v2\/posts\/237\/revisions\/238"}],"wp:attachment":[{"href":"https:\/\/www.resilence.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.resilence.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}