try catch
A try-catch block marks a block of statements to try, and specifies one or more responses should an exception be thrown. If an exception is thrown, the try-catch statement catches it.
File Location
We will be working in the following file:
The TRY Block
The try block will contain one or more statements. That is, you want the try block to succeed, and if it does not succeed, you want control to pass to the catch block. Let's start by creating a function and putting the try block inside of it:
Here we have function that takes in a parameter(we'll talk more about functions and parameters later). Let's call the function right below that last curly:
The CATCH Block
If any statement within the try block throws an exception, control will shift to the catch block. If no exception is thrown in the try block, the catch block is skipped. So here's some starter code for a catch block.
Notice that when we call this function a second time, it breaks. We can't make a 7 uppercase.
Illustration
Here's a starter illustration of the syntax:
Key Takeaways
This was a starter in checking for errors. You don't need to master this at this point. Just know that the try-catch block is a thing.
Last updated