Giter Site home page Giter Site logo

pangloss / defexception Goto Github PK

View Code? Open in Web Editor NEW

This project forked from redplanetlabs/defexception

0.0 1.0 0.0 19 KB

A simple Clojure library that allows one to dynamically define Java Exception classes in Clojure.

License: Eclipse Public License 1.0

Clojure 100.00%

defexception's Introduction

defexception

Build Status Clojars Project

A simple Clojure library that allows one to dynamically define Java Exception classes in Clojure.

Motivation

Sometimes you just want a quick Exception class that you can catch directly. defexception uses clojure.asm to dynamically create a Java class that directly inherits from clojure.lang.ExceptionInfo.

The created exception class does not add any behavior to clojure.lang.ExceptionInfo.

Usage

To create your own exception class you can do this:

(ns foo.bar
 (:require '[com.rpl.defexception :refer [defexception]]))

(defexception MyException)

This will create the foo.bar.MyException class that inherits from clojure.lang.ExceptionInfo. This will also import the class into the current namespace and create a helper function foo.bar/->MyException to help you construct the exception class.

Now you can do this:

(try
  (throw (->MyException "My bad!" {:my-bad 1}))
  (catch MyException e
    (ex-data e)))
;; => {:my-bad 1}

The generated ->MyException helper function has several signatures to help you instantiate your exception.

;; creates an exception with no message or ex-data
(->MyException) 

;; creates an exception with only ex-data
(->MyException {:hello 1}) 

;; creates an exception with only a message
(->MyException "My Bad!") 

;; creates an exception with both a message and ex-data
(->MyException "My Bad!" {:hello 1}) 

;; creates an exception with a message, ex-data and a cause
(->MyException "My Bad!" {:hello 1} (Exception. "The cause"))

AOT compilation

Care was taken to make these exceptions compatible with Clojure's AOT compilation.

License

Copyright © 2019 Red Planet Labs Inc.

Distributed under the Eclipse Public License version 1.0

defexception's People

Contributors

bhauman avatar nathanmarz avatar blak3mill3r avatar pangloss avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.