Javascript--javascript errors-Throw, Try, and Catch

Source: Internet
Author: User
Tags button type

JavaScript errors-Throw, Try, and Catch

JS break
JS Verification

The TRY statement tests the code block for errors.

The catch statement handles the error.

The throw statement creates a custom error.
Mistakes are bound to happen.

When the JavaScript engine executes JavaScript code, various errors occur:

It could be a syntax error, usually a coding error or typo caused by a programmer.

It may be a misspelling or a missing feature in the language (possibly due to browser differences).

The error may be caused by error output from the server or user.

Of course, it may also be due to many other unpredictable factors.
JavaScript throws an error

When an error occurs, the JavaScript engine usually stops and generates an error message when something goes wrong.

The technical term that describes this situation is that JavaScript throws an error.
JavaScript Testing and capturing

The Try statement allows us to define a block of code that performs error testing at execution time.

The catch statement allows us to define a block of code that executes when a try block of code has an error.

The JavaScript statement try and catch are paired occurrences.
Grammar

Try
{
Run the code here
}
catch (Err)
{
Handle the error here
}

Instance

In the following example, we deliberately write a typo in the code of the try block.

The catch block catches the error in the try block and executes the code to handle it.

<! DOCTYPE html>
<script>
var txt= "";
Function message ()
{
Try
{
Adddlert ("Welcome guest!");
}
catch (Err)
{
Txt= "There was a error on this page.\n\n";
txt+= "Error Description:" + err.message + "\ n";
txt+= "Click OK to continue.\n\n";
alert (TXT);
}
}
</script>

<body>
<input type= "button" value= "View message" onclick= "message ()" >
</body>



Throw statement

The throw statement allows us to create custom errors.

The correct technical term is: Create or throw an exception (exception).

If you use throw with try and catch, you can control the flow and generate custom error messages.
Grammar

Throw exception

An exception can be a JavaScript string, a number, a logical value, or an object.
Instance

This example detects the value of the input variable. If the value is wrong, an exception (error) is thrown. Catch catches this error and displays a custom error message:

<script>
function MyFunction ()
{
Try
{
var X=document.getelementbyid ("Demo"). Value;
if (x== "") throw "empty";
if (IsNaN (x)) throw "not a number";
if (x>10) throw "too high";
if (x<5) throw "too low";
}
catch (Err)
{
var Y=document.getelementbyid ("mess");
Y.innerhtml= "Error:" + Err + ".";
}
}
</script>

<p>please input a number between 5 and 10:</p>
<input id= "Demo" type= "text" >
<button type= "button" onclick= "MyFunction ()" >test input</button>
<p id= "Mess" ></p>


Note that if the getElementById function is faulted, the above example throws an error.


++++++++++++++++++++++++++++++++++++++++++++++++++++

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<script>
var txt= "";
Function message ()
{
Try
{
Adddlert ("Welcome guest!");
}
catch (Err)
{
txt= "This page has an error. \ n ";
txt+= "Error Description:" + err.message + "\ n";
txt+= "Click OK to continue. \ n ";
alert (TXT);
}
}
</script>

<body>
<input type= "button" value= "view message" onclick= "message ()"/>
</body>

++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<body>













<script type= "Text/javascript" >
function MyFunction ()
{
Try
{
var x = document.getElementById ("demo"). Value;
if ("") throw "value is null";
if (IsNaN (x)) throw "is not a number";
if (x>10) throw "value is too large";
if (x<5) throw "value is too small";
}

catch (Err)
{
var y = document.getElementById ("mess");
y.innerhtml = "Error:" + Err + ". ";
}
}

</script>
<p> Please enter a number from 5 to 10:</p>
<input id= "Demo" type= "text" >
<button type = "button" onclick = "myFunction ()" > Test input value </button>
<p id = "Mess" ></p>

</body>


Javascript--javascript errors-Throw, Try, and Catch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.