O'Reilly logo

Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation by James A. Henley, Matthew Johnson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required