JavaScript window onerror event

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In general we use try/catch statement to catch errors in JavaScript. If an error is raised by a statement that is not inside a try...catch block, the onerror event is fired.

message Specifies the error message.
URL Specifies the location of the file where the error occurred.
line Specifies the line number where the error occurred.

JavaScript window onerror event example

{
alert("Message : " + message + "\nURL : " + url + "\nLine Number : " + line);
// Return true to supress the browser error messages (like in older versions of Internet Explorer)
return true;
}

NonExistingFunction();

If the error is handled by a try/catch statement, then the onerror event is not raised. onerror event is raised only when there is an unhandled exception.
{
alert("Message : " + message + "\nURL : " + url + "\nLine Number : " + line);
// Return true to supress the browser error messages (like in older versions of Internet Explorer)
return true;
}

try
{
NonExistingFunction();
}
catch (e)
{
}

Output : 'NonExistingFunction' is undefined

onerror event handler method can also be used with HTML elements : In the example below, since the image is not existing and cannot be found we get "There is a problem loading the image" error.
[script type="text/javascript"]
function imageErrorHandler()
{
alert("There is a problem loading the image");
}
[/script]
Рекомендации по теме
Комментарии
Автор

Hi Venkat, you are God to all students who are trying to learn a new programming language. Thanks a ton!

TubeSwarn
Автор

Nice Tip kud venkat. Your video tuts are really good and the teaching flow is really admirable.

ArunMeansSun
Автор

Hi Venkat,
Your Videos are very Nice. It is like spoon feeding.Can you record a video on Project life cycle (how many members will be there in a project.how are they working and day to day work)

sudhakargoud