Theme Layout

Boxed or Wide or Framed

Wide

Theme Translation

Display Featured Slider

Featured Slider Styles

Display Grid Slider

yes

Grid Slider Styles

Display Trending Posts

Display Author Bio

Display Instagram Footer

off

Dark or Light Style

Light

Blog Archive

Powered by Blogger.

Wednesday, February 22, 2017

[JavaScript] Errors - Throw and Try to Catch


# JavaScript Errors - Throw and Try to Catch


The try statement lets you test a block of code for errors.
The catch statement lets you handle the error.
The throw statement lets you create custom errors.
The finally statement lets you execute code, after try and catch, regardless of the result.

● Errors will Happen!

When executing JSP code, different errors can occur.
Errors can be coding errors made by the programmer, errors due to wrong input, and other unforeseeable things.

● JavaScript try and catch

the try statement allows programmer to define a block of code to be tested for errors while it is being executed.
The catch statement allows programmer to define a block of code to be executed, if an error occurs in the try block.
The JSP statement try and catch come in pairs.

https://www.w3schools.com/js/js_errors.asp

● JavaScript Throws Errors

When an error occurs, JSP will normally stop and generate an error message.
The technical term for this is. JSP will throw and exception (throw and error).

JavaScript will actually create an Error object with two properties. name and message.

● The throw Statement

The throw statement allows programmer to create a custom error.
Technically programmer can throw an exception.
The exception can be a SP string, a number, a boolean or an Object.
If programmer use throw together with try and catch, programmer can control program flow and generate custom error messages.

● Input Validation Example

This example examines input. If the value is wrong, an exception is thrown.
The exception is caught by the catch statement and custom error message is displayed.

● HTML Validation

The code above is just an example.
Modern browsers will often use a combination of JSP and built-in HTML validation,
using predefined validation rules defined in HTML attributes.
<input id="prac" type="number" min="5" max="10" step="1">

● The finally Statement

The finally statement lets programmer execute code, after try and catch, regardless of the result.

● The Error Object

JavaScript has a built in error object that provides error information when an error occurs.
The error object provides two useful properties. name and message.

● Error Object Properties

https://www.w3schools.com/js/js_errors.asp

● Error Name Values

6 different values can be returned by the error name property.

https://www.w3schools.com/js/js_errors.asp

● Range Error

A RangeError is thrown if programmer use a number that is outside the range of legal values. 

● Reference Error

ReferenceError is thrown if programmer use a variable that has not been declared.

● Syntax Error

SyntaxError is thrown if programmer try to evaluate code with a syntax error.

● Type Error

A TypeError is thrown if programmer use a value that is outside the range of expected type.

● URI Error

A URI Error is thrown if programmer use illegal characters in a URI function.



QuickEdit
Unknown
0 Comments
Share This Post :

You Might Also Like

No comments:

Post a Comment

Follow @SunriseSunsetBlog