Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. # Exception Handling Exception is broadly classified into the following categories - [[Checked Exception]] - The classes that extend Throwable class except [[RuntimeException]] and [[Error]] are known as checked exceptions. E.g. [[IOException]], [[SQLException]], etc. Checked exceptions are checked at compile-time. - [[Unchecked Exception]] - The classes that extend [[RuntimeException]] are known as unchecked exceptions. For example, [[ArithmeticException]], [[NullPointerException]], [[ArrayIndexOutOfBoundsException]], etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime. ### Example <code> (ns clojure.examples.example (:gen-class)) ;; This program displays Hello World (defn Example [] (def string1 (slurp "Example.txt")) (println string1)) (Example) </code> #### Output <code> Caused by: java.io.FileNotFoundException: Example.txt (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at clojure.java.io$fn__9185.invoke(io.clj:229) at clojure.java.io$fn__9098$G__9091__9105.invoke(io.clj:69) at clojure.java.io$fn__9197.invoke(io.clj:258) at clojure.java.io$fn__9098$G__9091__9105.invoke(io.clj:69) </code> ### Example <code> (ns clojure.examples.example (:gen-class)) (defn Example [] (try (aget (int-array [1 2 3]) 5) (catch Exception e (println (str "caught exception: " (.toString e)))) (finally (println "This is our final block"))) (println "Let's move on")) (Example) </code> #### Output <code> caught exception: java.lang.ArrayIndexOutOfBoundsException: 5 This is our final block Let's move on </code> ## Error {{ https://www.tutorialspoint.com/clojure/images/exceptions_in_clojure.jpg }} ### Catching Exceptions <code> (try (//Protected code) catch Exception e1) (//Catch block) </code> ### Example <code> (ns clojure.examples.example (:gen-class)) (defn Example [] (try (def string1 (slurp "Example.txt")) (println string1) (catch Exception e (println (str "caught exception: " (.getMessage e)))))) (Example) </code> #### Output <code> caught exception: Example.txt (No such file or directory) </code> ## Multiple Catch Blocks ### Example <code> (ns clojure.examples.example (:gen-class)) (defn Example [] (try (def string1 (slurp "Example.txt")) (println string1) (catch java.io.FileNotFoundException e (println (str "caught file exception: " (.getMessage e)))) (catch Exception e (println (str "caught exception: " (.getMessage e))))) (println "Let's move on")) (Example) </code> #### Output <code> caught file exception: Example.txt (No such file or directory) Let's move on </code> ## Finally Block <code> (try (//Protected code) catch Exception e1) (//Catch block) (finally //Cleanup code) </code> ### Example <code> (ns clojure.examples.example (:gen-class)) (defn Example [] (try (def string1 (slurp "Example.txt")) (println string1) (catch java.io.FileNotFoundException e (println (str "caught file exception: " (.getMessage e)))) (catch Exception e (println (str "caught exception: " (.getMessage e)))) (finally (println "This is our final block"))) (println "Let's move on")) (Example) </code> #### Output <code> caught file exception: Example.txt (No such file or directory) This is our final block Let's move on </code> ### Example <code> (ns clojure.examples.example (:gen-class)) (defn Example [] (try (def string1 (slurp "Example.txt")) (println string1) (catch java.io.FileNotFoundException e (println (str "caught file exception: " (.toString e)))) (catch Exception e (println (str "caught exception: " (.toString e)))) (finally (println "This is our final block"))) (println "Let's move on")) (Example) </code> #### Output <code> caught file exception: java.io.FileNotFoundException: Example.txt (No such file or directory) This is our final block Let's move on </code> ## Refs - https://www.tutorialspoint.com/clojure/clojure_exception_handling.htm open/exception-handling.txt Last modified: 2024/10/05 06:15by 127.0.0.1