Reader Level:
ARTICLE

Try Catch Statement in TypeScript

Posted by Nitin Bhardwaj Articles | TypeScript November 30, 2012
In this article I will explain what is try catch statement and how to delete element from array in TypeScript with an example.
  • 0
  • 0
  • 8110

Try-Catch Statement

The try-catch 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
{
// Code which can cause an exception.
}
catch (Exception ex)
{
// Code to handle exception
}
 
We can also use the "finally" block for exception handling; see:

try
{
// Code which can cause an exception.
}
catch (Exception ex)
{
// Code to handle exception
}

finally
{
//
}
 
The "finally" block is guranteed 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 functionality 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

 

<!DOCTYPEhtml>

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>TypeScript HTML App</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/>

    <scriptsrc="app.js"></script>

</head>

<body>

    <h2>Delete Element From TypeScript Array</h2>

    <divid="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.


enter-element.jpg

 

Output 2


Enter elements.


element-0.jpg



element-1.jpg



element-2.jpg


Output 3


Enter the location of where you wish to delete an element:


enter-delete-element.jpg

 

Output 4


result.jpg

 

Output 5


If we enter a greater position element then:


err.jpg



err-1.jpg

COMMENT USING

Trending up