### Error handling

When we develop software or web applications we need to make sure that we handle errors, avoiding the program from breaking.

Using the example from the previous lesson, where we had a problem due to an undefined variable, let's add a new line of code at the end.

        function get_user(){

            window.setTimeout(function(){
                var u = {
                    'name': 'John'
                };
                return u;
            }, 1000);

        }

        function greet_user(user) {
            console.log('Hi ' + user.name + ', how are you?');
        }

        var user = get_user();
        greet_user(user);

        console.log('New line of code');

Note that due to the error, everything we write after is not executed. When this happens, all the functionality of our page can be compromised.

We can protect our application from breaking by using try and catch statements.

### Try/Catch

The Javascript interpreter tries to execute the code inside the try block. If something goes wrong, instead of throwing an error, it will jump to the catch block where we can add an exception and continue executing our code normally.

        try {
            var user = get_user();
            greet_user(user);
        } catch(err) {
            console.log('Hi, how are you?');
        }

        console.log('New line of code');

Through the 'err' argument, we can access the error message to debug our code. Actually you can name it anyway you want, but 'err' is a convention therefore it is recommended to follow it.

        try {
            var user = get_user();
            greet_user(user);
        } catch(err) {
            console.log(err);
            console.log('Hi, how are you?');
        }

        console.log('New line of code');

### Finally

after the try/catch statement, we can include a finally statement that will always be executed, no matter if we had an error or not.

        try {
            var user = get_user();
            greet_user(user);
        } catch(err) {
            console.log(err);
            console.log('Hi, how are you?');
        }     finally {
            console.log('This line will be executed in all cases.');
        }

### Throw

The throw function is used to throw custom errors inside the try/catch structure. Let's see an example where we don't have an actual error, but the result is not what we would expect:

        var user = {
            'name': ""
        }

        try {
            greet_user(user);
        } catch {
            console.log('Hi, how are you?');
        }

In this case there is a user variable with a name property therefore no error occurred. However, that property was empty, generating a weird message. To tackle this, we can generate our own error if the name is empty:

        function greet_user(user) {
            console.log('Hi ' + user.name + ', how are you?');
        }

        var user = {
            'name': ""
        }

        try {

            if (!user.name) {
                throw 'Name is empty';
            }

            greet_user(user);

        } catch (err) {
            console.log(err);
            console.log('Hi guest user, how are you?');
        }

If the interpreter hits a throw statement, it goes to the catch block directly.

### Contents