Try Catch-Handout
Try Catch-Handout
Error Handling
In the previous section, we assumed that the value received in the setter is a valid string.
We cannot call the split method on null or undefined, as it's only available to strings.
In these cases, we would need to add error handling.
Defensive Programming
In situations like this, we should do error handling at the beginning of the function or method.
This is what we call defensive programming.
We want to make sure that the values coming in are valid, they're in the right shape, so we
can execute our logic.
In the first line of this method, we use the typeof operator to check if the type of value is not
equal to (!==) string.
When we pass a null object, it no longer displays a message, and the object does not
change.
Displaying Errors
Sometimes we want to report an error in our application.
Throwing an exception is a technical jargon that you might hear in a lot of programming
languages.
Throwing an Exception
Instead of returning from this method, use the throw keyword and then create a new Error
object.
This Error object is a constructor function, as indicated by the use of Pascal Case.
When we are calling it, we are using the new keyword to create a new Error object.
As an argument to the Error constructor, we can pass a string as the error message:
‘Value is not a string’
This is just a plain JavaScript object, there's nothing special about this.
But the moment you throw this error, we refer to that as an exception.
Error Handling
Now we have some basic error handling in this method, by throwing the exception.
Now somewhere else we will need to catch that exception, and display the error.
A try block can accept 1 or more statements, with at least 1 being able to throw an
exception.
Next we add the catch block, and within parentheses, we pass an identifier.
In the catch block, we get the error object and do something with it.
We can display it on the console, which is only visible to developers, so an end user will not
see this error.
The proper way is to display a label, perhaps a red label on the user interface.
This is not something I'd recommend you to do, because that's a very old and poor way of
reporting errors to users.
When you go to the browser, you should see an alert like this:
Error: Value is not a string
More Error Handling
Instead of null, let’s pass in an empty string.
We don’t get an error, however firstName is set to an empty string, and lastName is
undefined, which is not desirable.
Ideally we want to make sure our users enter a first name and a last name.
When splitting the string, we can check the length of the parts array created.
Here we check if parts.length is not equal to 2, then something must be missing.
Next we throw another exception, so we throw a new Error with the message:
Enter a first and last name.
Recap
When we throw an exception, the lines after the throw statement are not executed.
The throw keyword will jump out of the method, and control flow will move to the catch block,
where we catch the exception and do something with it.
TASKS
1. Error Handling
In an earlier section, we did an exercise to count occurences of a number within an
array
This function has a problem, we are assuming this first argument is a valid array. If
we pass another value, like a boolean, we will get an UncaughtTypeError, because
we are using the array.reduce() method, which is not available to booleans.