To help me learn to write code using tests I've been practising this TDD Kata, courtesy of Roy Osherove: https://osherove.com/tdd-kata-1. The kata has the learner write a method 'Add', building up its functionality over several steps. Step 5 asks that if the method is called with a negative number, it should throw an exception.
Prior to this I had heard the expression 'to throw an error' and knew that exceptions were a thing in programming. I went to MDN to find out how I could code it for myself, and also to the Jest docs to work out how to write a test for it. Code written, tests passing, excellent.
A few days later, I returned to my recent take-home coding assignment to add some extra functionality. The first thing I wanted to add was some error handling, in order to validate the input to my program by checking it contained only the particular letters it would use during its execution. Normally I might console.log the error but I remembered my new discovery of throwing exceptions...good practise I thought, and wrote a test then a method to check the passed-in string, throwing an error if any forbidden characters were encountered.
Then I wondered what I had actually achieved by doing this. Why would I use throw rather than just console.log-ing an error message? To be perfectly honest I had no idea what value one held over the other and which was most appropriate for a particular situation. I had a vague sensation of having just employed an overly-sophisticated solution to my problem, showing off simply to practise this new concept.
Spoiler alert: After doing some more research, turns out I was kinda showing off but not even properly. Here's what I found out:
- try - use the try statement to define a block of code which you would like to be checked for errors when it is executed
- catch - if the try block of code resulted in an error, execute the code block defined with this statement
- finally - execute this block of code regardless of the result of the try block
'try' and 'catch' statements come in pairs. 'throw' statements can be placed inside a 'try' block to provide custom messages depending on the error: eg. you want to check user input from a form. If the user inputs an invalid character, the error message could be "input not recognised", or if they used the wrong case, it could be "input must be lower case".
In the 'catch' block you can grab that error that just occurred and do something with it...interpolate the message into a string perhaps with some added formatting, so you can show the user what they did wrong.
You could use 'finally' to always display the input back to the user so they can see whatever they just typed in.
I've paraphrased some of this excellent explainer from w3schools.com: (https://www.w3schools.com/js/js_errors.asp). It helped me get my head around it so if you also want to understand it I suggest you go there too. Use the 'try-it editor' to get some practise. I can definitely see the value of using 'try' and 'catch' with 'throw' to build some custom error-handling methods and will use it in the future, but I don't need it right now, in this project!