Expert Developer JavaScript Question:
Download Job Interview Questions and Answers PDF
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.
Download Expert JavaScript Developer Interview Questions And Answers
PDF
Previous Question | Next Question |
How to get height and width of different browser in Javascript? | What is the difference between == and === ? |