Try-Catch-Finally—Gracefully Handling Exceptions
The kind of exception we just dealt with is known as an unhandled exception; we allowed the code to break and reacted by fixing it. What if we wanted to deal with potential exceptions proactively?
This is where the Try-Catch-Finally statement comes in. It permits us to gracefully handle the errors in our code which, in turn, prevents the Console from being flooded with messages and may even prevent your game from locking up or crashing during runtime.
Nesting your code in a Try block just tells it to expect either a Catch or a Finally block of code immediately after. In the event that the code in a Try block would throw an exception, it immediately executes those subsequent code blocks. This block ...
Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.