CoffeeScript Exception Handling - CoffeeScript

What is CoffeeScript Exception Handling?

An exception (or exceptional event) is a trouble that arises all through the execution of an application. while an Exception takes place, the ordinary flow of this system is disrupted and the program/software terminates abnormally, which isn't always recommended, consequently, these exceptions are to be treated.
An exception can arise for plenty distinctive reasons. here are some situations wherein an exception occurs.

  • A user has entered invalid facts.
  • A record that wishes to be opened cannot be discovered.

Exceptions in CoffeeScript

CoffeeScripts helps exception/error handling the usage of the try catch and in the end blocks. The functionalities of these blocks are equal as in JavaScript, the try block holds the high-quality statements, the catch block has the motion to be completed while an exception happens, and the eventual block is used to execute the statements unconditionally.

Following are the syntaxes of try catch and finally blocks in CoffeeScript.

The try block should be followed through both exactly one catch block or one finally block (or one in every of each). while an exception occurs inside the attempted block, the exception is positioned in e and the capture block is executed. The non-compulsory in the end block executes unconditionally after attempt/capture.

example

the subsequent instance demonstrates the Exception coping with using try and catch blocks in CoffeeScript. In here, we're looking to use an undefined image in CoffeeScript operation and we dealt with the error happened using the attempt to catch blocks. keep this code in a record with the name Exception_handling.coffee

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output.

The finally block

We also can rewrite the above example using in the end block. If we achieve this, the contents of this block are achieved unconditionally after try and catch. Shop this code in a file with the name Exception_handling_finally.coffee

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output.

This is the statement of finally block

The throw Statement

CoffeeScript also supports the throw statement. You can use throw statement to raise your builtin exceptions or your customized exceptions. Later these exceptions can be captured and you can take an appropriate action.

Example

The following example demonstrates the usage of the throw statement in CoffeeScript. Save this code in a file with name

throw_example.coffee

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output.



All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CoffeeScript Topics