Web page Special Try...catch exception handling detailed
Grammar
try{
Statement1
}catch (Exception) {
Statement2
}
It can be used to handle all or some of the errors that can occur in a script.
If a mistake is not caused by a try ... Catch the statement, it is through the error that other reports can handle.
If no other report handles errors, it is passed to the browser for processing.
Statement1 may occur where this is a mistake, and statement2 is used to handle errors.
When an error occurs, its value is passed to the catch drop Point declaration and stored in the exception.
<title>using a try. Catch statement</title>
<script language= "JavaScript" >
<!--
function MyErrorHandler (data) {
try{
try{
if (data = = "string") {
Throw "E0";
}else{
Throw "E1";
}
}catch (e) {
if (E = = "E0") {
Return ("error (" + E + "): entry must is numeric.");
}else{
Throw e;
}
}
}catch (e) {
Return ("error (" + E + "): entry is invalid.");
}
}
function ProcessData (form) {
if (isNaN (parseint (form.mytext.value)) {
Alert (MyErrorHandler ("string"));
}else{
Alert ("You have correctly entered a number");
}
}
-->
</script>
<body>
<form name= "MyForm" >
Please enter a number:
<input type=text size=10 value= "name=" MyText ">
<input Type=button value= "process" name= "mybutton" onclick= ' ProcessData (this.form) ' >
</form>
</body>
Error handling. Catch that error
<title>catch that error!</title>
<script>
function Catcherror (errstring) {
try {
try {
if (errstring = = 1)
throw new error ( -1, "errstring is-1!");
Else
throw new error (0, "errstring is not-1!");
}
catch (e) {
if (E.number = = 1)
Return (E.description + "got this one!");
Else
Throw e;
}
}
catch (e) {
Return (e.description + "This one is not handled here!");
}
}
</script>
<body>
<form name= "Theform" >
<input type=text name=errtext value= "-1" >
<input Type=button name=btnthrow value= "Catch it!" onclick= "alert (Catcherror (Document.theform.errtext.value));" >
</form>
</body>
Exception handling and attempts exception handling with Try/catch
<title></title>
<script language= " JavaScript "type=" Text/javascript
function Getmonthname (monthnumber) {
throw " Invalidmonthnumber '
}
try {
alert (getmonthname)
}
catch (Exception) {
& nbsp; alert ("an" + Exception + "exception is encountered. " Contact the program vendor. ")
}
</script>
<body>
</body>