Expert Developer JavaScript Question:
How to manage exception handling in Javascript?
Answer:
► Javascript provides the features of exception handling using try..catch..finally blocks which is much similar to the C# exception handling blocks.
► We have to write the suspected code in try block and all the exceptions that occur in the try block will be caught in catch block.
► For example :
window.onload = function()
{
try
{
var v1=30;
var v2 =v1/v3;
}
catch(ex)
{
document.write(ex.name + " : " +ex.message);
}
Finally
{
document.write('this is finally block');
}
}
► This code snippet will cause an error :
TypeError : 'v3' is undefined.
► We have to write the suspected code in try block and all the exceptions that occur in the try block will be caught in catch block.
► For example :
window.onload = function()
{
try
{
var v1=30;
var v2 =v1/v3;
}
catch(ex)
{
document.write(ex.name + " : " +ex.message);
}
Finally
{
document.write('this is finally block');
}
}
► This code snippet will cause an error :
TypeError : 'v3' is undefined.
Previous Question | Next Question |
How to get height and width of different browser in Javascript? | What is the difference between == and === ? |