The try catch in TypeScript statement provides a way to handle some or all of the errors that may occur in an application. These errors are often referred to as an exception. In a try-catch statement, you code a try block that contains the statements that may throw an exception. Then, you code a catch block that contains the statements that are executed when an exception is thrown by any statement in the try block. This is known as exception handling.
If an exception is thrown within a try block, the exception is caught by the catch block. Otherwise, the exception is passed to the calling function. This passing of the exception continues until the exception is caught or the application ends with a runtime error.
Syntax
- try {} catch (Exception ex) {}
- We can also use the "finally"
- block
- for exception handling;
- see: try {}
- catch (Exception ex) {} finally {}
The "finally" block is guaranteed to be executed.
Example
The following example describes how to use try-catch statements in TypeScript. This program deletes an element from an array. Deleting an element from an array does not affect the size of the array. It also checks whether deletion is possible or not. For example, suppose an array has three elements and you want to delete an element at position seven which is not possible.
Step 1
Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window is opened. Provide the name of your application like "Delete_Element_from_Array", then click on the Ok button.
Step 2
After Step 1 your project has been created. Solution Explorer, which is at the right side of Visual Studio, contains the following:
- CSS: This is the design stylesheet file. Styles define how to display HTML elements.
- ts: This is the TypeScript file. All classes and functions are defined in this file.
- js: This is the JavaScript file. This file is created from the TypeScript file.
Step 3
Delete_Element_from_Array.ts
- class delete_element
- {
- element()
- {
- var pos, c, n, value;
- var array: number[] = [100];
- n = parseInt(prompt("Enter number of elements in array"));
- for (c = 0; c < n; c++)
- array[c] = parseInt(prompt("Enter " + n + " integers"));
- for (var x = 0; x < n; x++)
- {
- var span = document.createElement("span");
- span.style.color = "Green";
- span.innerText = "Enter " + x + " Element -> " + array[x] + "\n";
- document.body.appendChild(span);
- }
- pos = parseInt(prompt("Enter the location where you wish to delete element"));
- try
- {
- if (pos >= n + 1)
- {
- alert("Deletion not possible.");
- }
- else
- {
- for (c = pos - 1; c < n - 1; c++)
- array[c] = array[c + 1];
- var span1 = document.createElement("span1");
- span1.style.color = "red";
- span1.innerText = "Resultant array is \n";
- document.body.appendChild(span1);
- for (c = 0; c <= n - 1; c++)
- {
- var span = document.createElement("span");
- span.style.color = "Blue";
- span.innerText = "Array Element " + c + " -> " + array[c] + "\n";
- document.body.appendChild(span);
- }
- }
- }
- catch (Error)
- {
- alert(Error.message);
- }
- }
- }
- window.onload = () =>
- {
- var greeter = new delete_element();
- greeter.element();
- };
deleteDemo.html
- <!DOCTYPE html>
- <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta charset="utf-8" />
- <title> TypeScript HTML App
- </title>
- <link rel="stylesheet" href="app.css" type="text/css" />
- <script src="app.js"></script>
- </head>
- <body>
- <h2> Delete Element From TypeScript Array
- </h2>
- <div id="content" />
- </body>
- </html>
app.js
- var delete_element = (function()
- {
- function delete_element() {}
- delete_element.prototype.element = function()
- {
- var pos;
- var c;
- var n;
- var value;
- var array = [100];
- n = parseInt(prompt("Enter number of elements in array"));
- for (c = 0; c < n; c++)
- {
- array[c] = parseInt(prompt("Enter " + n + " integers"));
- }
- for (var x = 0; x < n; x++)
- {
- var span = document.createElement("span");
- span.style.color = "Green";
- span.innerText = "Enter " + x + " Element -> " + array[x] + "\n";
- document.body.appendChild(span);
- }
- pos = parseInt(prompt("Enter the location where you wish to delete element"));
- try
- {
- if (pos >= n + 1)
- {
- alert("Deletion not possible.");
- }
- else
- {
- for (c = pos - 1; c < n - 1; c++)
- {
- array[c] = array[c + 1];
- }
- var span1 = document.createElement("span1");
- span1.style.color = "red";
- span1.innerText = "Resultant array is \n";
- document.body.appendChild(span1);
- for (c = 0; c <= n - 1; c++)
- {
- var span = document.createElement("span");
- span.style.color = "Blue";
- span.innerText = "Array Element " + c + " -> " + array[c] + "\n";
- document.body.appendChild(span);
- }
- }
- }
- catch (Error)
- {
- alert(Error.message);
- }
- };
- return delete_element;
- })();
- window.onload = function()
- {
- var greeter = new delete_element();
- greeter.element();
- };
Output 1
Enter the number of elements in the array and then click on ok.
Output 2
Enter elements.
Output 3
Enter the location of where you wish to delete an element:
Output 4
Output 5
If we enter a greater position element then: