throws-exception

0.0.3 • Public • Published

throws-exception

This npm package includes 2 functions to check whether a function throws an exception or not:

  • throwsException
  • doesntThrowException

Example usage:

import { throwsException, doesntThrowException } from 'throws-exception';

if(throwsException(() => { throw 'ERROR!'; })) {
  console.log('An error occurred');
}

if(doesntThrowException(() => { throw 'ERROR!'; })) {
  console.log("This won't be printed");
}

The implementation of these functions is very simple:

exports.throwsException = function(f) {
  try {
    f();
  } catch(e) {
    return true;
  }
  return false;
}

exports.doesntThrowException = function(f) {
  try {
    f();
  } catch(e) {
    return false;
  }
  return true;
}

Package Sidebar

Install

npm i throws-exception

Weekly Downloads

0

Version

0.0.3

License

ISC

Unpacked Size

2.34 kB

Total Files

4

Last publish

Collaborators

  • adriancuadrado