All Projects → rufoa → try-let

rufoa / try-let

Licence: other
Better exception handling for Clojure let expressions

Programming Languages

clojure
4091 projects

Projects that are alternatives of or similar to try-let

Catch Exception
Stars: ✭ 137 (+705.88%)
Mutual labels:  exception-handling
catchr
catchr: Flexible, useful tools for dealing with conditions in R, for new users and veterans
Stars: ✭ 17 (+0%)
Mutual labels:  exception-handling
laravel-email-exceptions
Email Exceptions package for Laravel 5.x
Stars: ✭ 33 (+94.12%)
Mutual labels:  exception-handling
Object Oriented Programming Using Python
Python is a multi-paradigm programming language. Meaning, it supports different programming approach. One of the popular approach to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).
Stars: ✭ 183 (+976.47%)
Mutual labels:  exception-handling
snq
A utility function to avoid type errors when traversing over arrays and object properties.
Stars: ✭ 30 (+76.47%)
Mutual labels:  exception-handling
Coderr.Client
Core client library for Coderr
Stars: ✭ 23 (+35.29%)
Mutual labels:  exception-handling
Faux Pas
A library that simplifies error handling for Functional Programming in Java
Stars: ✭ 100 (+488.24%)
Mutual labels:  exception-handling
mwe-cpp-exception
Minimum working example of proper C++11 exception handling
Stars: ✭ 20 (+17.65%)
Mutual labels:  exception-handling
cakephp-error-email
ErrorEmail Plugin for CakePHP3.x
Stars: ✭ 16 (-5.88%)
Mutual labels:  exception-handling
stack-trace-art
Turning programming exceptions into art
Stars: ✭ 39 (+129.41%)
Mutual labels:  exception-handling
Exceptions4c
🐑 An exception handling framework for C
Stars: ✭ 189 (+1011.76%)
Mutual labels:  exception-handling
Express Typescript
Express + TypeScript + Boilerplate for Web / API App
Stars: ✭ 230 (+1252.94%)
Mutual labels:  exception-handling
AspNetCore.FriendlyExceptions
ASP.NET Core Filter and Middleware to catch exceptions and translate them into nice HTTP responses
Stars: ✭ 17 (+0%)
Mutual labels:  exception-handling
Graphql Errors
Simple error handler for GraphQL Ruby ❗️
Stars: ✭ 170 (+900%)
Mutual labels:  exception-handling
quarkus-resteasy-problem
Unified error responses for Quarkus REST APIs via Problem Details for HTTP APIs (RFC7807). Supports Quarkus 2.0+ and 1.4+
Stars: ✭ 36 (+111.76%)
Mutual labels:  exception-handling
Stacktracey
Parses call stacks. Reads sources. Clean & filtered output. Sourcemaps. Node & browsers.
Stars: ✭ 115 (+576.47%)
Mutual labels:  exception-handling
bugsnag-java
Bugsnag error reporting for Java.
Stars: ✭ 51 (+200%)
Mutual labels:  exception-handling
react-error-guard
⚛️An overlay for displaying stack frames based on create-react-app/packages/react-error-overlay
Stars: ✭ 18 (+5.88%)
Mutual labels:  exception-handling
DuckOS
Such OS; Very Duck!
Stars: ✭ 16 (-5.88%)
Mutual labels:  exception-handling
ignition-stackoverflow
An Ignition tab that fetches StackOverflow questions and provides a searchbar.
Stars: ✭ 74 (+335.29%)
Mutual labels:  exception-handling

try-let

try-let is a Clojure macro designed to make handling some exceptions slightly nicer. It acts like let, but allows you to catch exceptions which may be thrown inside the binding vector. Exceptions thrown inside the body of the try-let are deliberately ignored.

Build Status

Installation

try-let is in Clojars. To use it in a Leiningen project, add it to your project.clj dependencies:

Clojars Project

then require try-let in your code:

(ns my.example
   (:require [try-let :refer [try-let]]))

Motivation

It can be quite difficult to combine try/catch with let properly. Clojure pushes you towards one of two patterns, neither of which is ideal.

(try
   (let [value (func-that-throws)]
      (act-on-value value))
   (catch Exception e
      (log/error e "func-that-throws failed")))

In the above pattern, the scope of the try/catch is too great. In addition to func-that-throws, it also affects act-on-value.

(let [value
   (try (func-that-throws)
      (catch Exception e (log/error e "func-that-throws failed")))]
   (act-on-value value))

In the above pattern, the scope of the try/catch is correct, affecting only func-that-throws, but when an exception is caught, act-on-value is evaluated regardless and must handle the exceptional case when value is nil.

Use

With try-let, we can instead do:

(try-let [value (func-that-throws)]
   (act-on-value value)
   (catch Exception e
      (log/error e "func-that-throws failed")))

This allows the scope of the try/catch to be made as precise as possible, affecting only func-that-throws, and for evaluation to only proceed to act-on-value when value is obtained without error. In this way, try-let can be thought of as similar to if-let, where the body is only evaluated when the value of the binding vector is not nil.

You can have multiple catch stanzas for different exceptions. Much of what you'd expect to work in a normal let works:

(try-let [val-1 (risky-func-1)
          val-2 (risky-func-2 val-1)]
   (log/info "using values" val-1 "and" val-2)
   (* val-1 val-2)
   (catch SpecificException _
      (log/info "using our fallback value instead")
      123)
   (catch RuntimeException e
      (log/error e "Some other error occurred")
      (throw e))
   (finally
      (release-some-resource)))

As an alternative, you can also put catch stanzas before other body expressions:

(try-let [val-1 (risky-func-1)]
  (catch Exception e
    (log/error e "Problem calling risky-func-1")
    0)
  (try-let [val-2 (risky-func-2 val-1)]
    (catch Exception e
      (log/error e "Problem calling risky-func-2")
      0)
    (log/info "using values" val-1 "and" val-2)
    (* val-1 val-2)))

This makes the code logic more linear, where exceptions are handled closer to where they appear.

Slingshot support

There is also a try+-let macro which is compatible with slingshot-style catch stanzas.

License

Copyright © 2015-2019 rufoa

Distributed under the Eclipse Public License, the same as Clojure.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].