npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

try-catch-finally

v2.0.2

Published

[![Travis CI](https://img.shields.io/travis/c24w/try-catch-finally.js.svg?style=flat-square)](https://travis-ci.org/c24w/try-catch-finally.js 'Build') [![Coveralls](https://img.shields.io/coveralls/c24w/try-catch-finally.js.svg?style=flat-square)](https:/

Downloads

54

Readme

try-catch-finally.js

Travis CI Coveralls David npm


Installation

Browser

<script src="try-catch-finally.js"></script>
console.log(typeof window.try) // -> function
console.log(typeof window._try) // -> function

AMD

define(['try-catch-finally'], function (_try) {
  console.log(typeof _try); // -> function
});

Node

npm install --save try-catch-finally
var _try = require('try-catch-finally');
console.log(typeof _try); // -> function

Usage

API

_try ( tryBlock )

  • tryBlock<function> - code which may throw errors

.catch ( [ error, ] handleError )

  • error <any> - (optional) error to catch
  • handleError <function> - handle errors which correspond to error (if defined); else handle any error

.finally ( finallyBlock )

  • finallyBlock <function> - code which will always exectue

Examples

Catch anything

_try(function () {
  throw new Eroror('boom');
})
.catch(function (e) {
  console.log('Caught', e);
});

Error value matches by strict equality (===).

Catch-by-value

_try(function () {
  throw 12345;
})
.catch(12345, function (e) {
  console.log('Caught', e);
});

Error value matches by strict equality (===).

Catch-by-name

_try(function () {
  throw { error: 'boom' };
})
.catch('object', function (e) {
  console.log('Caught', e);
});

Error name matches similarly to typeof, with the bonus that it:

  • is case-insensitive
  • works for boxed primitives (e.g. new String())

Catch-by-type

_try(function () {
  throw 'boom';
})
.catch(Object, function (e) {
  console.log('Caught', e);
});

_try(function () {
  throw new TypeError('boom');
})
.catch(Error, function (e) {
  console.log('Caught', e);
});

Error type matches similarly to instanceof, with the bonus that it works for literal primitives ('hello', 123, etc).

Warnings

Catch-by-name may not work

It's not always possible to get the name of an object in JavaScript, such as for objects created using non-native constructors:

function CustomError() {}

_try(function () {
  throw new CustomError();
})
.catch('CustomError', function (e) {
  console.log('Caught CustomError: ' + e);
});

Or for some native objects which use inheritance:

_try(function () {
  throw new TypeError();
})
.catch('TypeError', function (e) {
  console.log('Caught TypeError: ' + e);
});

Those catch blocks won't execute. The best this library can do is find out that:

  • the new CustomError() is some kind of object, but not specifically a CustomError by name
  • the new TypeError() is some kind of Error, but not specifically a TypeError by name

It's best to use the catch-by-type style in those cases.

Catch-by-type won't work across frames/processes

This quirk exists in the native instanceof, which fails across browser frames and node processes when the instance's constructor is different one passed to instanceof. It's best to use the catch-by-name in those cases.

Errors are consumed

Any error thrown synchronously in the try block is consumed by this library. There are two ways to ensure errors which aren't caught/handled by any catch don't disappear:

Use an indiscriminate catch block

_try(function () {
  throw new Error('boom');
})
.catch(String, function (e) {
  // Catch doesn't apply
})
.catch(function (e) {
  // Handle all other errors
});

Use a finally block

This will cause any unhandled error to be re-thrown:

_try(function () {
  throw new Error('boom');
})
.catch(String, function (e) {
  // Catch doesn't apply
})
.finally(function () {
  // Error is re-thrown after finally block
});