The try…catch construct is a frequently utilized statement across numerous programming languages. Essentially, it serves the purpose of managing sections of code that are susceptible to errors. Initially, it evaluates the code for any potential issues it might have, and subsequently, it executes measures to address those issues if they arise. An effective programming strategy is to encapsulate intricate code within the try…catch blocks.
We will examine each section of the statement one by one:
The try{} statement: In this context, the code that requires potential error evaluation is contained within the try block. If an error arises, control is transferred to the catch{} block, where appropriate measures are taken to address the error. If no error occurs, the code contained within the try block is executed as intended.
The catch{} statement: This segment manages errors that may arise in the code by executing the statements defined within it. Within this block, you can find either a custom exception handler or a predefined handler. The execution of this block occurs solely when there is an error in the code that requires attention, which is indicated by the preceding try block. If no errors occur, the catch block will not be executed.
Note: catch {} statement executes only after the execution of the try {} statement. Also, one try block can contain one or more catch blocks.
Syntax:
try{
expression; } //code to be written.
catch(error){
expression; } // code for handling the error.
try…catch example
<html>
<head> Exception Handling</br></head>
<body>
<script>
try{
var a= ["34","32","5","31","24","44","67"]; //a is an array
document.write(a); // displays elements of a
document.write(b); //b is undefined but still trying to fetch its value. Thus catch block will be invoked
}catch(e){
alert("There is error which shows "+e.message); //Handling error
}
</script>
</body>
</html>
Throw Statement
The throw statement is utilized for generating user-defined exceptions. Users have the capability to create and throw their personalized error types. Upon the execution of the throw statement, any subsequent statements will not be executed. Instead, control will immediately transition to the corresponding catch block.
Syntax:
throw exception;
try…catch…throw syntax
try{
throw exception; // user can define their own exception
}
catch(error){
expression; } // code for handling exception.
An exception may take the form of a string, a number, an object, or a boolean value.
throw example with try…catch
<html>
<head>Exception Handling</head>
<body>
<script>
try {
throw new Error('This is the throw keyword'); //user-defined throw statement.
}
catch (e) {
document.write(e.message); // This will generate an error message
}
</script>
</body>
</html>
Utilizing the throw statement, developers are able to generate custom errors.
try…catch…finally statements
The finally block is an optional segment of code that runs after the execution of both the try and catch blocks. This block is not contingent upon whether an exception occurs. Regardless of whether an exception is raised or not, if a finally block is included, the code within it will certainly be executed. It operates independently of the output generated.
Syntax:
try{
expression;
}
catch(error){
expression;
}
finally{
expression; } //Executable code
try…catch…finally example
<html>
<head>Exception Handling</head>
<body>
<script>
try{
var a=2;
if(a==2)
document.write("ok");
}
catch(Error){
document.write("Error found"+e.message);
}
finally{
document.write("Value of a is 2 ");
}
</script>
</body>
</html>
Consequently, it is also possible to utilize the try/catch/throw/finally keywords in conjunction to manage intricate code scenarios.