How to catch any JavaScript exception in Clojurescript?

ClojureScript is a dialect of the Clojure programming language that compiles to JavaScript, allowing developers to use Clojure's functional programming paradigms in web browsers and Node.js environments. When building ClojureScript applications, proper exception handling is crucial for maintaining application stability and user experience.

What is ClojureScript?

ClojureScript is a compiler that transforms Clojure code into optimized JavaScript. Originally, Clojure only compiled to Java Virtual Machine bytecode, but ClojureScript emerged in 2011 to bring Clojure's powerful features to client-side development.

ClojureScript operates at a higher abstraction level than JavaScript. While JavaScript deals with variables, loops, conditions, and objects, ClojureScript focuses on expressions, collections, sequences, and transformations. The key difference lies in syntax and approach, but both ultimately execute in JavaScript environments.

Syntax Comparison

Here's how function calls differ between JavaScript and ClojureScript:

Object-oriented JavaScript:

employeeRecords.findOne("employee details");

Functional JavaScript:

findOne(employeeRecords, "employee details");

ClojureScript:

(findOne employeeRecords "employee details")

In ClojureScript, the opening parenthesis moves to the left of the function name, and commas between arguments are omitted.

Exception Types in ClojureScript

ClojureScript handles two main categories of exceptions:

  • Checked Exceptions: Errors that extend Throwable class (IOException, SQLException) - checked at compile time

  • Unchecked Exceptions: Runtime errors like NullPointerException, ArithmeticException, ArrayIndexOutOfBoundException - checked at runtime

Basic Exception Example

When attempting to read a non-existent file, ClojureScript throws an exception:

(ns clojureScript.examples
   (:gen-class))

(defn Example []
   (def employee (slurp "Employee.txt"))
   (println employee))

(Example)

This code attempts to read "Employee.txt" using the slurp function. If the file doesn't exist, it throws a FileNotFoundException.

Using try/catch/finally

ClojureScript provides the try special form that compiles to JavaScript's try/catch construct:

(ns clojureScript.examples
   (:gen-class))

(defn Example []
   (try
      (def employee (slurp "Employee.txt"))
      (println employee)
      (catch Exception e (println (str "caught exception: " (.toString e))))
      (finally (println "This block always executes")))
   (println "Continuing execution"))

(Example)

Handling Array Index Exceptions

Here's an example handling ArrayIndexOutOfBoundsException:

(ns clojureScript.examples
   (:gen-class))

(defn Example []
   (try
      (aget (int-array [1 2 3 4]) 5)
      (catch Exception e (println (str "caught exception: " (.toString e))))
      (finally (println "This block always executes")))
   (println "Continuing execution"))

(Example)

This code attempts to access index 5 in an array with only 4 elements, triggering an ArrayIndexOutOfBoundsException.

Multiple Catch Blocks

You can handle different exception types with multiple catch blocks:

(ns clojureScript.examples
   (:gen-class))

(defn Example []
   (try
      (def string1 (slurp "Example.txt"))
      (println string1)
      (catch java.lang.ArrayIndexOutOfBoundsException e 
         (println (str "caught array exception: " (.getMessage e))))
      (catch Exception e 
         (println (str "caught general exception: " (.getMessage e))))
      (finally (println "Final block always executes")))
   (println "Continuing execution"))

(Example)

Key Points

  • The catch block allows custom error handling logic

  • The finally block executes regardless of whether exceptions occur

  • Multiple catch blocks can handle different exception types specifically

  • Exception handling prevents application crashes and improves user experience

Conclusion

ClojureScript's exception handling mirrors JavaScript's try/catch mechanism while maintaining Clojure's functional programming paradigms. Proper exception handling ensures robust applications that gracefully manage errors and continue execution when possible.

Updated on: 2026-03-15T23:18:59+05:30

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements