JavaScript · Lesson 25 / 32 · தமிழ்
Error Handling
கருத்தைப் புரிந்துகொள்ளுங்கள். Code-ஐ மாற்றி இயக்குங்கள். ஒரு சிறிய பயிற்சியால் கற்றதை உறுதி செய்யுங்கள்.
மூலப் பாட விளக்கங்கள் தமிழில் உள்ளன. கூடுதல் concept, practice மற்றும் answer notes ஆங்கிலத்தில் உள்ளன.
முக்கியக் கருத்து
Errors describe failed operations. Catch errors where you can recover or add useful context; do not silently discard them. finally runs after try/catch for cleanup.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
விரிவான விளக்கங்கள்
JavaScript-ல Error Handling
Errors inevitable. நல்ல error handling app crash ஆகாம தடுக்கும், users-க்கு meaningful feedback கொடுக்கும்.
try / catch / finally
try {
// Error வரலாம்ன்னு நினைக்குற code
const data = JSON.parse("invalid json");
} catch (error) {
console.error("Error:", error.message);
console.error("Type:", error.name); // "SyntaxError"
} finally {
// எப்பவும் run ஆகும் — cleanup-க்கு
console.log("Done.");
}Error Types
| Error Type | எப்போ வருது |
|---|---|
| SyntaxError | Invalid JavaScript syntax |
| ReferenceError | Declare ஆகாத variable use பண்ணும்போது |
| TypeError | Wrong type (non-function-ஐ call பண்ணும்போது) |
| RangeError | Value valid range-க்கு வெளியே இருக்கும்போது |
Custom Errors Throw பண்றது
class ValidationError extends Error {
constructor(message, field) {
super(message);
this.name = "ValidationError";
this.field = field;
}
}
function validateAge(age) {
if (typeof age !== "number") {
throw new ValidationError("Age must be a number", "age");
}
}
try {
validateAge("old");
} catch (err) {
if (err instanceof ValidationError) {
console.log(`Field: ${err.field} — ${err.message}`);
} else {
throw err; // unexpected errors-ஐ re-throw பண்ணு
}
}முழுமையான உதாரணம்
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
try {
JSON.parse("not valid JSON");
} catch (error) {
console.log(error.name);
} finally {
console.log("Finished checking");
}எதிர்பார்க்கும் முடிவைப் பாருங்கள்
SyntaxError Finished checking
மாற்றி இயக்கிப் பாருங்கள்
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Editor load ஆனதும் code-ஐ ஏற்றி Run அழுத்துங்கள்.
OneCompiler என்ற இலவச வெளிப்புற சேவை code-ஐ இயக்குகிறது. Run செய்யும் code அங்கு அனுப்பப்படும். தனிப்பட்ட தகவல்கள் அல்லது secret keys உள்ளிட வேண்டாம்.
சிறிய பயிற்சி
Practice challenge
Replace the input with valid JSON and log the parsed title.
விடை மற்றும் விளக்கம்
Parse '{"title":"JavaScript"}' and log .title. The catch block is skipped; finally still runs.
தவிர்க்க வேண்டிய தவறு: A synchronous try/catch does not catch a later rejected promise unless that promise is awaited inside the try block.
அடுத்த பாடத்திற்குத் தயாரா?
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.