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:
If you have any questions, feel free to ask me in the comments!
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:
try { // Try to execute this code name = javax.swing.JOptionPane.showInputDialog(null, "Enter name"); }
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:
String name = ""; char firstChar = ''; try { name = javax.swing.JOptionPane.showInputDialog(null, "Enter name"); firstChar = name.charAt(0); } catch (StringIndexOutOfBoundsException outofbounds) { // React to a specific type of exception System.out.println("Please make a longer input!"); } catch (Exception e) { System.out.println("Undefined error occurred!"); }
So you could imagine the try-catch structure just like an if-else-block:
if(this_block_doesnt_work) { // some critical code, like I/O } else if(a_specific_error_occurs) { // react to this error, e.g. tell the user // what went wrong } else { // Any other error occurred. }
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.
Table of contents
Part 1 – MyFirstProgram
Part 2 – Access Modifiers and Variables
Part 3 – Methods
Part 4 – Arrays
Part 5 – Control flow
Part 6 – Classes and Objects
Part 7 – Exceptions and exception handling (You are here)
Addendum 1 – IntelliJ QuickTip
Addendum 2 – Important programming concepts explained
9 thoughts on “Programming Basics 7 – Exceptions and exception handling”