Skip to content

Latest commit

 

History

History
40 lines (33 loc) · 733 Bytes

README.md

File metadata and controls

40 lines (33 loc) · 733 Bytes

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;
}