Handle Your Javascript Errors And check the comments of your code.
1.try…catch
the try…catch method is the best way to handle javascript code error checks. we should use the try method to try our code and use the catch method to check is an error on the code. for example,
try {
// try for the code...
}
catch (err) {
// handle error or show the error here...
}
2. error object
the error object method is used for when an error occurs, JavaScript generates an object containing the details about it. The object is then passed as an argument to catch. for example,
try {
lalala; // error, variable is not defined!
}
catch (err) { alert(err.name); // ReferenceError
alert(err.message); // lalala is not defined
// Can also show an error as a whole
// The error is converted to string as "name: message"}
3. Optional catch binding
optional catch binding method is used to try or catch method. It is used to throw the try method and not to see errors. for example,
try {
// ...code
}
catch {
// without (err)
// ...
}
4. Using try…catch in json
as we know about try catch method . we can use the JSON files on the try catch method. for example,
const json = '{"name":"Jack", "age": 30}';try {
const user = JSON.parse(json);
alert(user.name);
}
catch(err){
alert(err.message);
}
5. Throwing our own errors
in the try… catch method, we can throw our own error also. own error means,if execute error on the code, we provide a error message of our own.for example,
const data= '{"name":"Jack", "age": 30}';try {
const user = JSON.parse(data);
alert(user.email); // email haven't on the data
}
catch(err){
alert('you passed a wrong data');
}
6.Rethrowing
In the example above we use try...catch
to handle incorrect data. But is it possible that another unexpected error occurs within the try {...}
block? Like a programming error (variable is not defined) or something else, not just this “incorrect data” thing.
For example:
let json = ‘{ “age”: 30 }’; // incomplete data
try {
let user = JSON.parse(json);
}
catch (err) {
alert("JSON Error: " + err); // JSON Error: ReferenceError: user is not defined
//(no JSON Error actually)
}
7. try…catch…finally
The try…catch construct may have one more code clause:finally.
If it exists, it runs in all cases:
- after try, if there were no errors,
- after catch, if there were errors.
for example,
try {
... try to execute the code ...
}
catch (err) {
... handle errors ...
}
finally {
... execute always ...
}
8.Global catch
Let’s imagine we’ve got a fatal error outside of try…catch, and the script died. Like a programming error or some other terrible thing.
Is there a way to react on such occurrences? We may want to log the error, show something to the user (normally they don’t see error messages), etc. for example,
window.onerror = function(message, url, line, col, error) { alert(`${message}\n At ${line}:${col} of ${url}`); };
9.Summery
The try...catch
construct allows handling runtime errors. It literally allows to “try” running the code and “catch” errors that may occur in it.
for example,
try {
// run this code
}
catch (err) {
// if an error happened, then jump here
// err is the error object }
finally {
// do in any case after try/catch
}
10.Bad comment
as a developer sometimes we need to comment out of our codes. sometimes we commented our so many codes. so that, a another developer can’t read write and understand of code. this is a bad habit and this called bad comment. for example,
function showPrimes(n) {
nextPrime:
for (let i = 2; i < n; i++) {
// check if i is a prime number
for (let j = 2; j < i; j++) {
if (i % j == 0) continue nextPrime;
} alert(i);
}
}
11.Good comment
good comments are useful for the coder and another developer. this type of code helps to know about code. which code used for why. as like that. the good comment provides a high-level overview of components, how they interact, what’s the control flow in various situations… In short — the bird’s eye view of the code. for example,
/**
* Returns x raised to the n-th power.
*
* @param {number} x The number to raise.
* @param {number} n The power, must be a natural number.
* @return {number} x raised to the n-th power.
*/
function pow(x, n) { ... }