1.0.0 • Published 9 years ago

xcore-exceptions v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

Exception.io: JavaScript

A collection of custom exception classes written in JavaScript.

Inheritance Hierarchy

Error
└── Exception
    ├── ArgumentException
    ├── InvalidInputException
    ├── NotImplementedException
    ├── NotSupportedException
    ├── NotFoundException
    ├── UnauthenticatedException
    └── UnauthorizedException

Classes

Usage

Initiate any one of the exception classes.

if (item == null) {
    throw new NotFoundException("Item not found.");
}

Check the type of exception we are dealing with:

var err = new Exception();

err instanceof Exception               // → true
err instanceof Error                   // → true
Exception.prototype.isPrototypeOf(err) // → true
Error.prototype.isPrototypeOf(err)     // → true
err.constructor.name                   // → "Exception"
err.name                               // → "Exception"
err.message                            // → "No description"
err.toString()                         // → "[Exception] No description"
err.stack                              // → prints the stack trace

Handle specific exception types using exception's constructor property:

try {
  foo("valA", "valB");
}
catch (e) {
  if (e instanceof ArgumentException) {
    console.error(e.toString());
  }
  else if (e instanceof InvalidInputException) {
    console.error(e.toString());
  }
  else {
    console.error(e.toString());
  }
}

Supported Platforms

Creator

License

Exception.io is released under the MIT license. See LICENSE for details.