Programming Basics 7 – Exceptions and exception handling

Introduction

Welcome to the last part of the programming basics series! In this series we’ll cover what exceptions are and how you deal with them.

If you’ve made all the examples, that are featured throughout the series, you might have encountered something like this:

This particular runtime error is caused by a wrong user input. It’s not caused by faulty code. Errors like this usually crash your application and a user has to restart it, in order to continue using it. In this part we’ll cover how to deal with such errors.

Solution to the last exercise

As the code is longer than in the last examples, you can view the files here:

What are exceptions?

Exceptions are raised when an application runs and something goes wrong, for example a user input is not what you expected during development. In the video above (and in the code provided as the solution to the last exercise), the application simply awaits any input with more than zero characters. But I didn’t write anything, therefore the applications gets an input, that is zero characters long. So you could say exceptions occur whenever a runtime error happens but they also give you informations about what happened.

Now you know what exceptions do, but what are they? Well in Java, they are objects. And they represent a specific error. The error seen in the video is an IndexOutOfBounds exception. This means, that I tried to access an Index of the string (the input), that doesn’t exist.

Dealing with exceptions

Exceptions happen. It’s that simple. You can’t prevent a user from making invalid inputs, even though you tell him, what to input. You can almost be 100% sure that someone will either try to make invalid inputs on purpose or they typed in an invalid value. But how can you react on such invalid inputs?

You can place any critical code (some code that might cause an error during runtime, for example I/O operations) in a so called try-block:

Your application will try to execute the code inside the block without any errors. If it works, everything’s fine, if not, you have to catch the exception, before it crashes your program. this is done with the so called catch-block:

// If an error happens in the try-block, execute the
// code in the catch-block
catch (Exception e)
{ }

Note that I wrote Exception e as the catch-block’s argument. You can exchange the Exception with a more specific type of exception (like IndexOutOfBoundsException). This makes sense if you want to react on a specific error. If you just react on Exception, it will work for any runtime error that occurs, because all other Exceptions are ‘children’ of this Exception class (We’ll discuss this topic in a later series!). You could also stack multiple blocks together to react on different types of errors:

Conclusion

This was a short introduction to exceptions and how to deal with them. There are user-defined exceptions, but we’ll cover these later, in a different series, because it somehow is a more advanced topics.