2.8 Java Script Exception Handling
2.8 Java Script Exception Handling
Agenda
Introduction
Types of Errors
JavaScript Errors Categorization
throw Statement
try Statement
catch Statement
finally Statement
Types of Exception Handling Statements
Course Outcomes:
CO. Outcome K Level
(Apply) Understand and apply JavaScript concepts for dynamic web page
CO2 K3
design
CO3 (Apply) Understand and apply shell commands and GIT workflow K2
Challenge time:
➢ Form Validation
➢ Page Redirect
Introduction:
➢ Exception handling is a process or method used for handling
the abnormal statements in the program and executing them.
➢ It also enables to handle the flow control of the program.
➢ For example, the Division of a non-zero value with zero will result
in infinity always, and it is an exception. Thus, with the help of
exception handling, it can be executed and handled.
➢ Exception Handling is implemented using throw{}, try{}, catch{}
and finally{}.
Types of Errors:
➢ Syntax Error: When a user makes a mistake in the pre-defined
syntax of a programming language, a syntax error may appear.
➢ Runtime Error: When an error occurs during the program’s
execution, such an error is known as Runtime error. The codes
which create runtime errors are known as Exceptions. Thus,
exception handlers are used for handling runtime errors.
➢ Logical Error: An error that occurs when there is any logical
mistake in the program that may not produce the desired
output and may terminate abnormally. Such an error is known
as a Logical error.
Error Object:
➢ When a runtime error occurs, it creates and throws an Error
object.
➢ Such an object can also be used as a base for the user-defined
exceptions.
➢ An error object has two properties:
❖ name: This object property sets or returns an error name.
❖ message: This property returns an error message in the string form.
throw{} statement:
➢ Throw statement is used for throwing user-defined errors.
➢ User can define and throw their own custom errors.
➢ When a throw statement is executed, the statements present
after it will not execute. The control will directly pass to the
catch block.
➢ Syntax:
throw exception;
try{} statement:
➢ The code that needs possible error testing is kept within the try
block.
➢ If any error occurs, it passes to the catch{} block for taking
suitable actions and handling the error. Otherwise, it executes
the code written within.
catch{} statement:
➢ catch block handles the error of the code by executing the set
of statements written within the block.
➢ catch block contains either the user-defined exception handler
or the built-in handler.
➢ This block executes only when any error-prone code needs to
be handled in the try block. Otherwise, the catch block is
skipped.
➢ catch {} statement executes only after executing the try
{} statement. Also, one try block can contain one or
more catch blocks.
finally{} statement:
➢ Finally is an optional block of statements that is executed after
the execution of try and catch statements.
➢ Finally block does not hold for the exception to be thrown.
➢ Any exception is thrown or not, finally block code, if present, will
definitely execute. It does not care for the output either.