For a basic example of a try catch block I have a sting that represents an invalid JSON string. In a real example this json might be pulled from a file that might end up being malformed for one reason or another, but foe the sake of this simple example it is just a string literal. So then because the JSON is in fact invalid, when I attempt to try to parse the JSON string in the try block of the try catch statement into a workable object, this results in an Error as expected. Inside the body of the catch block I have it so I just log the resulting error message that is a property of the error object.
Instead of the script failing completely a try catch statement can be used to define what to do in the event of an error such as this. The flow of the script will then continue instead of coming to a griding halt.
Although I am just logging the message to the console, in a real project I would of course want to do something more than just that. Say I am working on a project where I need to load a json file or some other kind of file that serves as a config file of sorts. I would want to not just log and error message to the console, I might want to do something more such as look at another file system location for a backup file of sorts or revert to using some hard coded settings on top of finding some way to alert the user to what is going on.
A try catch statement must be followed by a catch or finally statement or else it will result in an Error. So it is possible to define a try statement without a catch but just a finally and that will work as one might expect.
When using the return keyword in the body of a try catch block, any return statement used in the finally block will supersede any additional return statements that may exist in the try, or catch blocks.
The catch block of a try catch statement will have a reference to the error object or catch id of the error that occurred in the try block. This can be used to set custom error handling conditions for specific errors.
So the basics of try catch statements are not so hard to get up to speed with. The typical use case situation is that you do something that might case an error in the try block, and then use the catch block to do what needs to happen in the event of an error. There is just the question if there is any weird situations about try catch that might come up now and then, and as I become aware of more things about that I will of course expand this post as needed.