Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2K views

Error Handling in JavaScript

Error handling

Uploaded by

PRINCE RAJ
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Error Handling in JavaScript

Error handling

Uploaded by

PRINCE RAJ
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Error handling in JavaScript

Error handling in JavaScript


• JavaScript is a loosely-typed language.
• It does not give compile-time errors.
• So some times you will get a runtime error for
accessing an undefined variable or calling
undefined function etc.
• JavaScript provides error-handling mechanism
to catch runtime errors using try-catch-finally
block, similar to other languages like Java or C#.
Error handling in JavaScript
try
{
// code that may throw an error
}
catch(ex)
{
// code to be executed if an error occurs
}
finally{
// code to be executed regardless of an error occurs or not
}
Error handling in JavaScript
try: wrap suspicious code that may throw an
error in try block.
catch: write code to do something in catch block
when an error occurs. The catch block can have
parameters that will give you error information.
Generally catch block is used to log an error or
display specific messages to the user.
Error handling in JavaScript
finally: code in the finally block will always be
executed regardless of the occurrence of an
error. The finally block can be used to complete
the remaining task or reset variables that might
have changed before error occurred in try block.
throw: use throw keyword to raise a custom
error.

You might also like