1

When deciding if to ignore, handle of re-throw errors in a Try Catch structure, what is the best way to uniquely identify the exact error?

I can't find any standard error number, do I have to parse the name and message properties?

EDIT: In the example below, I want to check if the error is due to the lack of a property on listEvent who's key is the value of evnt.type. If this is the source of the error, I want to ignore it, otherwise I want to re-throw it to let it bubble up.

The only way I can think of is sort of duck-typing the error like this...

try{
    listEvent[evnt.type](procedure)
}catch(error){
    if (error.message.match(evnt.type)) {
        console.log(error.name + ' ' + error.message + ' - ignored')
    } else {
        throw error
    }
}
7
  • Well, which error are you looking for? There are no "standard error numbers" in JS, everyone rolls his own thing (mostly building Error subclass structures)
    – Bergi
    Commented Oct 9, 2014 at 16:12
  • @Bergi Im looking to uniquely identify which standard error occurred so that I can decide what to do about it. So that I can direct the code to handle, accept or re-throw it. Is there a canonical method for doing this in JS? Or, as stated in my question, do I just parse the message?
    – Cool Blue
    Commented Oct 9, 2014 at 16:21
  • Are these custom exceptions that you are throwing? Commented Oct 9, 2014 at 16:36
  • @VivinPaliath No, standard errors
    – Cool Blue
    Commented Oct 9, 2014 at 16:45
  • I would suggest checking for the exceptional condition rather than letting it throw. It is far easier that way. See the second part of my answer. Commented Oct 9, 2014 at 16:47

1 Answer 1

1

Create a custom exception and then use instanceof:

function IllegalArgumentException(message) {
    this.message = message;
}

You also want to make it extend the Error prototype:

IllegalArgumentException.prototype = new Error();
IllegalArgumentException.prototype.constructor = Error;

Which you can then use like so:

throw new IllegalArgumentException("Argument cannot be less than zero");

You can then check the type using instanceof:

try {
    // Some code that generates exceptions
} catch (e) {    
    if (e instanceof IllegalArgumentException) {
        // Handle this
    } else if (e instanceof SomeOtherTypeOfException) {
        // Handle this
    }
}

You can also add any other property you want to the constructor of the exception.

As far as your example goes, I'm not sure what you're trying to do. listEvent[evnt.type] will return undefined if event.type is not a property or a key in listEvent. It is better to see if evnt.type even exists by doing something like this:

if (typeof listEvent[evnt.type] !== "undefined") {
   listEvent[evnt.type](procedure);
}
5
  • "listEvent[evnt.type] will return undefined if event.type is not a property or a key in listEvent". That may be so but listEvent[evnt.type]() - note the invocation - throws an error.
    – Cool Blue
    Commented Oct 9, 2014 at 16:50
  • 1
    @CoolBlue Of course it would. You are trying to call an undefined value as you would a function. I'm saying that it is better to check and see if you even have a property by that name before you call it. This way you don't have to deal with checking to see what was in the exception. Commented Oct 9, 2014 at 16:53
  • OK, thanks for the advice... seems to me like six of one and half a dozen of the other though. I guess the answer to my question is that there is no simple, reliable way to uniquely identify a standard error and its not normal practice to try. Personally, I prefer the try catch structure because it is only executed if the code fails. Your suggestion fires every time and this is only more efficient if the "exception" is the rule, which in my case its not. Cheers.
    – Cool Blue
    Commented Oct 9, 2014 at 17:03
  • Unfortunately there is no way to identify a standard error in the fashion that you want. IMHO my suggestion is better because you are not using exceptions as control-flow. Exceptions are used in exceptional circumstances and shouldn't be used for business logic. The performance penalty that you will pay for an exception is far greater than the check you perform, especially in the degenerate case where every evnt.type you use doesn't exist in listEvent. Commented Oct 9, 2014 at 17:07
  • 1
    This strikes me as an elegant and readable solution to a common need. Not sure why it doesn't have more upvotes!
    – Chris
    Commented Mar 23, 2015 at 16:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.