Exception Handling in JavaScript in asp.net C#


Exception handling is a very important concept in programming technology. In earlier versions of JavaScript, the exceptions handling was not so efficient and programmers found it difficult to use. Later versions of JavaScript resolved this difficulty with exceptions handling features like try.
 
Catch handlers, which presented a more convenient solution for programmers of JavaScript.

JavaScript allows you to trap errors using the standard “try-catch” exception-handling block.

<script type=”text/javascript”>
try{
var x = NonExistentFunction();
document.write(x);
}
catch(err){
alert(“We got an error in the code”);
}
</script>

Similarly we can raise errors by using the throw keyword. The throw statement allows you to create an exception. If you use this statement together with the try…catch statement, you can control program flow and generate accurate error messages.

<script type=”text/javascript”>
try {
var exfor=10
if(exfor!=20)
{
throw “Missmatch Error”;
}
}
catch(e){
if (e == “Missmatch Error”) {
LogError(e);
}
else {
alert(“An unexpected error found”);
}
Function LogError(e) {
“Send the error to the Handler file(.ashx file) and you can log the error in a file, database or at any other location.”
}
</script>