JS Error Handling
JS Error Handling
Syntax Error: Is called as parsing errors,occurs at compile time fortraditional programming languages at
interpret time for JavaScript. Following example causes a syntax error because it is missing a closing
parenthesis.
ex:
<body>
<script type="text/javascript">
document.write(; <-------syntax error
</script>
</body>
Runtime Errors: These are called exceptions and these errors occurred at execution time. The following
example causes a run time error because here syntax iscorrect but at run time it is trying to call a non
existed method.
Eg:
<body>
<script type="text/javascript">
document.writeli("hello welcome");
</script>
</body>
Logical errors: These can be most difficult error to find.These errorsoccurred if you make a mistake in
the business logic.These errors unableto handle.
<script>
//finding area of circle
var r=10;
var area=3.14*r*2;
alert(area);
</script>
Try..catch statement: This statement allows you to test a block of codefor errors.The try block contains
the code to be run & the catch blockcontains the code to be executed if an error occurs.
Syntax:
try{
code to run[break;]
}
catch(ob){
code to run if an exception occurs[break;]
}
Syn: excep.name
excep.message
excep.description
List of Errors:
Error Name Description
EvalError An error has occurred in the eval() function (old ver)
RangeError A number "out of range" has occurred
ReferenceError An illegal reference has occurred
SyntaxError A syntax error has occurred
TypeError A type error has occurred
URIError An error in encodeURI("uri") has occurred
Note:
> writing only try is syntax error.
> writing only catch is also syntax error.
> we can write any no.of try-catch blocks, and any where(within the script tag or
external js).
> try block is successfully executed then catch block not executed.
>if try block is faired/thrown an error then only catch block is executed.
> order of writing is 1st try block and then catch block. means don't change order
writing(otherwise it becomes error);
> one try is allowing only one catch block.
ex:
<head>
<script type='text/javascript'>
alet("welcome to exceptions");
alert("Thank you");
</script>
</head>
No o/p
ex:
<head>
<script type='text/javascript'>
try{
alrt("welcome to exceptions");
}
catch(e){
alert(e.description);
}
</script>
</head>
eg:
<head>
<script type='text/javascript'>
try{
var x=prompt("Enter value to evaluable");
alert(eval(x));
}catch(e){
alert("sorry Alpha-Invalid:"+e.description);
}finally{
alert("This Block Always get executed"); }
alert("next")
}
</script>
</head>
Throw statement: This statement allows to you create an exception.If you usethis statement together
with try catch statement,you can control programflow and generate accurate error message.The
exception can beString, integer,boolean or an object.
Syn:
throw excepobj;
throw "text";
Throw Exception
<body>
<script type='text/javascript'>
var x=prompt("Enter Any number")
try{
if(x>10) {
throw "Err1";
}
else if(x<=10){
throw "Err2";
}
else if(isNaN(x)){
throw "Err3";
}
}catch(err){
if(err=="Err1"){
document.write("Error:The value is too height");
}
if(err=="Err3"){
document.write("Error:The value is not a number");
}
}
</script>
</body>