Skip to content

Function that returns a boolean indicating whether an exception was thrown or not

Notifications You must be signed in to change notification settings

ProperlyExplained/throws-exception

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Function that returns a boolean indicating whether an exception was thrown or not

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published