Reputation: 2812
In Python, if I run into an error (e.g., function argument is wrong data type, can't operate on data because it is formatted wrong, etc.), I print an error to the terminal and run sys.exit()
to kill it. I haven't gotten around to learning about error handling, exception handling, or whatever it is, so that is why I use that method. JavaScript error/exception/whatever handling looks simple, but will raising or throwing halt the code's execution? If it doesn't, then how would I do so?
Say I only want a function (and, by extension, the whole set of code) to work if the page I am on matches a specific URL. Calling the function when the page is the wrong URL should log a message to the console and halt the script's execution. How do I do that? Just have the test in the try
block? Do I need to employ some special syntax used for handling errors/exceptions?
Upvotes: 1
Views: 4467
Reputation: 19301
Calling the function when the page is the wrong URL should log a message to the console and halt the script's execution. How do I do that?
If the URL is unacceptable throw an error with a description of the problem. e.g.
throw new Error("operation x not supported at this page location");
This results in the message being logged to the console. Ensure the absence of a controlling try...catch statement around the throw
statement or, or around any function calls leading to it, to halt execution when an error is thrown.
As mentioned in comment, throwing an exception will not cancel call backs yet to be made from asynchronous operations which have been initiated but not completed.
If a try...catch
statement is in effect when an error is thrown, execution resumes with the controlling catch statement without further execution of (other) code following the throw
statement, and without resumption of intermediary function calls.
A function that executes a throw
statement without catching it inside the function does not return. That is to say it does not return a value and resume execution immediately after the call made to it in calling code.
If a user written try block surrounds a direct or indirect call to the function throwing the error, the call stack is restored to that in effect when the try block was executed. Any stack frames created for function calls within the try block are popped off the call stack and discarded without further execution of called functions. Execution resumes with the catch block associated with the try block, passing the error thrown as the catch parameter, or in the finally block if no catch block was provided. The system does not automatically log errors thrown inside a try block to the console.
If no user written try block is found, all call stack frames are popped, the thread dies and the error thrown is logged to the console. Hence throwing an error outside a try block stops execution without calling an exit function.
The equivalent of try...cacth
in Python seems to be try...except
with a raise
statement used for throw
with slightly different syntax.
Upvotes: 3