0

Let's say I have this code:

var myVar = 0;


function myFunctionOne() {
    myVar = myVar + 2;
    if(myVar <= 3) {
        alert("all is good");
    } else {
        showError(myVar);
    }
}

function myFunctionTwo() {
    myVar = myVar + 2;
    if(myVar <= 3) {
        alert("all is good");
    } else {
        showError(myVar);
    }
}

function myFunctionThree() {
    //This should never run....
    myVar = myVar + 2;
    if(myVar <= 3) {
        alert("all is good");
    } else {
        showError(myVar);
    }
}

function showError(myVar) {
    alert("Error Var is larger than 3. Var is" + myVar);
    return false; 
//This doesn't seem to stop anything

}

myFunctionOne();
myFunctionTwo();
myFunctionThree();

Here is also the fiddle: http://jsfiddle.net/dzjk44Lr/

What can I put inside my showError() function, that will kill any subsequent function calls? In this example, myFunctionThree(); should never run.

I know this is a simple example, but I'm trying to get my head around the module pattern, and in my module I have a lot of functions that delegate work, and modify variables. So if one function fails, or is given an illegal variable, I want everything to stop and show the user an error. Because if there was an error in myFunctionOne() for example, there is no point in continuing to execute any of the other code.

I guess, I'm looking for something like the php exit() function. Any advice about how to do this, or how I should do it differently would be greatly appreciated.

1
  • You can't do something like php exit() because you're not going to shut-down the browser. This all sounds to me like you need an error handling design for your code rather than a throw your hands up in the error and just abort everything. You can throw an exception and that will stop the current thread of execution (up to any exception handler that might catch the exception), but is not always the best way to design code. Commented Aug 25, 2015 at 23:35

2 Answers 2

3

You can throw an error:

throw new Error("Error Var is larger than 3. Var is" + myVar);
Sign up to request clarification or add additional context in comments.

10 Comments

@Amir - throw has it's place in Javascript if used properly. It sounds to me more like you need to design proper error handling into your functions so one can encounter an error, return that error to its callers and let them decide how to proceed or not based on the error.
@Amir - It sounds like you need to study some Javascript basics about returning a value from a function and then testing that return value in the caller. You can start with this page about error handling, but it sounds like you really need to study/learn more Javascript basics about return values from functions. showError() can return a value that the function calling it can test (likely using if () statements) so the caller of another function can make decisions based on what the function returns.
@Amir - a real life example might be you make a function call see if a password meets the minimum requirements (say it's called checkPassword()) and the function returns a result that indicates whether the password contains the necessary complexity and length to meet the rules. The caller of that function examines the return value of the function and then based on that result, it either informs the user that they need a different password or it proceeds on with the rest of the process.
"Where it says all functions need to be independent." Well, checkPassword and savePassword seem to be independent. The function that calls both of them is the glue. if (valid) { thenSave(); } is a perfectly understandable solution. The "module pattern" is for organizing code as a whole. Individual parts still have closer internal (and looser external) dependencies. You can't (realistically) write code without dependencies.
@Amir—only use try..catch where you expect an error that you can't detect or avoid, e.g. in early IE it was impossible to detect support for XMLHttpRequest without actually calling it, so the call was wrapped in try..catch.
|
1

You can use javascript throw statement.

According to Mozilla MDN

Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.

Example:

throw "An error ocurred."

You can also go pro and throw an error object like this:

throw new MyAppError("custom error str", 128);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.