How to catch any JavaScript exception in Clojurescript?


In our code, usually, we will get an issue with errors. Errors will disturb the normal flow of our application. So, it is required to handle error exceptions in our code to implement any application in any programming language. Now, we will see how to handle exceptions in Clojurescript. Before that, first we learn what Clojurescript is.

What is ClojureScript?

At the fundamental level, Closurescript is an accent of the closure programming language that is used to compile to JavaScript. Actually, closure originally compiled only to Java virtual machine bytecode, later in 2011 Closurescript came as an option to bring closure to client-side development.

Closurescript is constructed at a higher level than JavaScript. In JavaScript, usually, deal with variables, loops, conditions, functions, arrays, and objects. When it comes to Closurescript, we deal with expressions, collections, sequences, and transformations. While we look at many features of Clojurescript that make it different from JavaScript.

With a few minor differences, They’re effectively the same language. The difference is the target environment. Closure is built on the Java Virtual Machine and integrates with Java. ClojureScript compiles to JavaScript and runs in the browser or Node.js environments.

Now, let’s see an example of how we make function calls in JS and in ClosureScript.

In Object-oriented JS function call −

employeeRecords.findOne("employee details");

Here, employeeRecords is an object, findOne is a function and "employee details" is arguments.

In JavaScript function call -

findOne(employeeRecords, "employee details");

Here, findOne is a function and employeeRecords, "employee details" are arguments.

Same example, we look into for Closurescript

(findOne employeeRecords "employee details");

Here, findOne function and employeeRecords "employee details" are arguments. In Clojurescript the only difference from the functional JavaScript code is that the left parenthesis is moved to the left and there is no comma between arguments.

One of the most important features of the Clojurescript compiler is that it produces google Closure modules, and it then makes use of the Closure compiler used to optimize the JavaScript.

Handling error exceptions

In error exceptions, we have two categories like

  • The class that extends the Throwable class expects RuntimeException i.e. checked exception. Examples like IOException, SQLException, etc. These are checked while compilation.

  • The classes that extend RuntimeExceptions i.e. unchecked exceptions like NullPointerException, ArithmeticException, ArrayIndexOutOfBoundException. They are checked at runtime instead of compilation time.

Example

Let's take an example.

(ns clojureScript.examples
   (:gen-class))
;; This program displays employee details
(defn Example []
   (def employee (slurp "Employee.txt"))
   (println employee))
(Example)

Output


Here, slurp is used to read all the content in the file. We can observe that the Employee.txt file is not there. So it will throw an output as FileNotFoundException.

Handling with try/catch/finally

Handling error exceptions in Clojurescript is more similar than it is different from handling them in JavaScript. ClojureScript provides a special form called try that compiles down to a try/catch construct in JavaScript. Let's see an example

Example

(ns clojureScript.examples
   (:gen-class))
;; This program displays employee details
(defn Example []
   (try
      (def employee (slurp "Employee.txt"))
      (catch Exception e (println (str "caught exception: " (.toString e))))
      (finally (println "This is our final block, it will execute always")))
      (println "Let's move on"))
(Example)

Output


Here, Employee.txt is a file that does not exist. FileNotFoundException handled by the catch block.

Example

Let's check another example, when we try to access a greater index that does not exist in an array.

(ns clojureScript.examples
   (:gen-class))
;; This program displays employee details
(defn Example []
   (try
      (aget (int-array [1 2 3 4]) 5)
      (catch Exception e (println (str "caught exception: " (.toString e))))
      (finally (println "This is our final block, it will execute always")))
      (println "Let's move on"))
(Example)

Output


Here, we try to access 5 indexes from an array [1 2 3 4], So ArrayIndexOutOfBoundException is handled by the catch block.

In the catch block, you can write our custom code to handle our exception. So that we can run our application as error free.

The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.

We can use multiple catch blocks to handle multiple types of exceptions. Each catch block depends on the type of exception raised on our code.

Example

Let's take an example

(ns Clojurescript.example
   (:gen-class))
(defn Example []
   (try
      (def string1 (slurp "Example.txt"))
      (println string1)
      (catch java.lang.ArrayIndexOutOfBoundsException e (println (str "caught file exception: " (.getMessage e))))
   (catch Exception e (println (str "caught exception: " (.getMessage e))))
      (finally (println "This is our final block, it will execute always")))
      (println "Let's move on"))
(Example)

Output


Use any clojure IDE online editors to execute this program. Like this we will handle exceptions in ClosureScript. Hope this article gives knowledge on Closurescript and error handling in Clojurescript.

Updated on: 08-Dec-2022

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements