Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I differentiate between different types of exceptions? [closed]

Tags:

javascript

I'm starting to learn JavaScript, so far no problem but I have a hard time finding a good explanation of the Exception mechanism in JS.

It seems similar to C++, JS allows to throw about every object, instead of just throwing an Exception object (probably due to it's dynamic nature).

throw 'An error occured.';

works, as well as

throw new Exception('An error occured.');

catch and finally both seem to work like their Java equivalent. Still, I don't know what are widely accepted best practices regarding exceptions.

So, for example, is it legit to throw objects of type string, like:

throw 'An error occured';

How would I differentiate between different types of exceptions?

like image 470
helpermethod Avatar asked Jan 24 '12 12:01

helpermethod


People also ask

What are the two types of exceptions?

There are mainly two types of exceptions in Java as follows: Checked exception. Unchecked exception.

How do you catch different types of exceptions?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

What are the two types of exceptions in Java which are the differences between them?

Difference Between Checked and Unchecked Exceptions in Java To summarize, the difference between a checked and unchecked exception is: A checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime.


2 Answers

The "best practice", I suppose, is to throw the correct type of Error object related to the problem causing the exception. ECMAScript defines several types of exception object, all of which inherit from Error. These objects are EvalError, RangeError, ReferenceError, TypeError and URIError.

Those constructors are used by native ECMAScript functions, which enables you to do something like this:

try {
    // do something
}
catch (e) {
    if (e instanceof TypeError) {
        // do something else
    }
}

Generally, using the throw statement without using an exception object strikes me as poor practice for several reasons, including:

  • Code designed to handle exceptions may be expecting an Error object, receiving a primitive may result in unexpected side effects or the inability to handle the exception without modifying the handling code. An example of this is the lack of a stack property on the result from the throw expression.
  • When not used in a try/catch statement, Internet Explorers 8 and lower will throw a different exception on your attempt to throw, with the message, "Exception thrown and not caught"*. This can be even more confusing if you're debugging using the developer tools or have a global exception handler set to window.onerror.

So, yes, in general stick to properly throwing instances Error or its inheriting objects.

*nb, IE also does this for types of Error object that aren't constructed directly from Error. Yes, I know that's stupid, but they fixed it in IE 9 even though they told me it was "by design".

like image 114
Andy E Avatar answered Sep 29 '22 06:09

Andy E


Throwing and catching exceptions is pretty expensive, and with JavaScript, you're primarily going to get exceptions in cases like when you try to parse a JSON string that is malformatted. I would suggest avoiding try/catch as much as possible and instead focus on checking for errors the old fashion way (return types, making sure variables are properly initialized before using them, etc.) since exceptions are less likely to happen here than in C++ or especially Java or .NET.

Andy E's recommendation is a good one for actually handling them, but in general, you should try to write JavaScript code defensively so that you don't even need try/catch. Remember, even JITed JavaScript in Chrome (fastest engine) is still slow as heck compared to Java or C#, to say nothing of C++, so anything that is expensive in those languages is likely to be even more so in JavaScript.

like image 29
Mike Thomsen Avatar answered Sep 29 '22 05:09

Mike Thomsen