This package has been deprecated

Author message:

this package is never useful

better-try
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Better Try-Catch Block

Install

npm i better-try

Concept

The native try...catch... block in JavaScript can only have one catch block, and no way to bind accurate error type to catch. Using better-try, you can have multiple catch block, and catch exact error type as you'll do in other programming languages.

Example

const betterTry = require("better-try");

betterTry(() => {
    // TODO
}).catch(TypeError, err => {
    console.log("TypeError occurred:", err.message);
}).catch(ReferenceError, err => {
    console.log("ReferenceError occurred:", err.message);
}).catch(RangeError, err => {
    console.log("RangeError occurred:", err.message);
}).catch(Error, err => { // catch any error that extends Error
    console.log(err.name, "occurred:", err.message);
}).catch(err => { // catch any type of thrown error
    console.log("error occurred:", String(err));
}).finally(() => {
    // TODO
});
// ...

Notice

If try to catch Error when there are other error types to catch, make sure that Error is on the bottom, otherwise it will be first caught and break catching any other type of error.

Package Sidebar

Install

npm i better-try

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

16.8 kB

Total Files

7

Last publish

Collaborators

  • ayonli