Chapter 12. Try & Catch
Bud has obtained the keys he needed from Vicente’s office. They’re going to be essential for executing the plan. Leaving the prison won’t be easy, but returning to it without anyone noticing will be even more complicated.
It appears that one of the staff members working in the prison hospital has been stealing some medications to sell among the inmates. The idea is to blackmail him to help us get me back in. It shouldn’t be too difficult; he’s sure to cooperate without problems.
Once outside, when it’s time to return, I’ll hide in the trunk of his car and we’ll simply drive into the employee parking lot. From there, accessing the library unseen is very simple. I’ll have to hide until the daily library visit time and then blend in with the group.

The guard in question is called Phil and he won’t help us without reason. In fact, if he learns about our escape plans, he would report us immediately. We need evidence of his wrongdoing; once we have it, he’ll have no choice but to cooperate.
Bud will take care of that. He has many resources and it seems he has been closely following Phil’s activities. For now, I don’t have to do anything, although I’ve already been warned that my contribution will be crucial at the end of the escape.
We already have an approximate date for the final phase of our plan. In two weeks there will be some holidays, and security measures tend to be reduced. Not by much, but at least that gives us some advantage. With this information, I can now coordinate with Chani’s cousin and tell him more or less which day I’m going to get out.
Once this range of days is specified, I hope Chani will take care of contacting Rich and arranging a meeting. I don’t know how we’re going to get the confession; I guess the idea is to record audio with the phone, but since I’m practically in isolation, I still don’t have more details.
Yesterday I had a long conversation with Bud, and he told me what he plans to do when our paths separate. Pedro and he have been planning the escape for a long time. It’s an opportunity they can’t let slip away. I don’t know exactly how old they are, but I estimate they’re around 50.
They still have a considerable amount of their sentence left and don’t want to spend the rest of their lives locked up. They have contacts outside and will leave the country the same day they get out. The plan is to be very far away once the guards realize they’ve escaped. I hope everything goes well.

We need to get to work and obtain that evidence that incriminates Phil. For this, we need to reach the prison hospital and, for that, I’m going to need to use some Java concepts that I haven’t learned yet. So, without me saying anything, Bud has offered to give me a new lesson. He’s a great teacher, so I can’t refuse.
Introduction and Importance of try and catch Blocks in Java
In Java, try and catch code blocks are used to handle exceptions. Exceptions are situations that can occur while a Program is running and could interrupt the normal flow of Program execution. Therefore, to avoid this, we must take into account these possible events and control them in advance.
These blocks are absolutely necessary as they are an organized and controlled way of handling errors and exceptions during Program execution. Thanks to try and catch, we will avoid possible errors in our Program that we didn’t even account for.
Most Common Exceptions
- Division by Zero: By wrapping the division inside a try block, you can catch the ArithmeticException and display an appropriate error message.
- String to Number Conversion: Use try-catch to catch NumberFormatException and handle invalid inputs.
- Array Index Access: Protect array element access with try-catch to catch ArrayIndexOutOfBoundsException.
- Safe Arithmetic Operations: Use Math.subtractExact inside a try block to catch ArithmeticException in case of overflow.
- String Concatenation: Wrap the concatenation loop in a try block to catch OutOfMemoryError and handle the error.
Structure of try-catch blocks
try block
try {
// Code that might generate an exception
}
catch block
It’s placed directly after the try block and has this structure:
catch (ExceptionType e) {
// Code to handle the exception
}
- Basic example
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0; // This throws ArithmeticException
System.out.println("The result is: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
}
}
Multiple catch Blocks
You can have multiple catch blocks to handle different types of exceptions. Each catch handles a specific exception.
try {
// Code that can throw different exceptions
} catch (ArithmeticException e) {
// Handle ArithmeticException
} catch (NullPointerException e) {
// Handle NullPointerException
}
The finally Block (Optional): You can add a finally block after the catch blocks. The finally block always executes, regardless of whether an exception was thrown or not, and is useful for releasing resources such as closing files or database connections.
try {
// Code that might throw exceptions
} catch (Exception e) {
// Handle any exception
} finally {
// Code that always executes, with or without exception
}
Summary of What We’ve Learned
As I mentioned at the beginning of the topic, in Java, try and catch blocks are fundamental for managing unexpected situations during Program execution, preventing interruptions in its normal development.
In simple exercises, it might not be necessary to use this structure, but when Programs become more complex, it’s necessary to consider all possible events that may occur during the execution of our code. These blocks allow us to handle, among other things, errors such as division by zero, incorrect string conversions, out-of-range array accesses, and arithmetic operations that could exceed limits.
At this point, the try-catch structure shouldn’t be very complicated for you. Inside the try block, we find the code that could generate an exception. If this occurs, that’s when the catch block comes into action and handles the error. You can have multiple catch blocks for different types of exceptions and optionally use the finally block to execute code that must always run, such as resource release.