- Lab
- Core Tech

Guided: Exception Handling
In this lab, you will learn how to handle exceptions in Java using try, catch, and finally blocks. These tools allow your applications to respond appropriately to errors, handle unexpected inputs gracefully, and continue providing a smooth user experience. By completing this lab, you will gain the skills to write code that can recover from errors and exceptions, making your programs more robust and reliable.

Path Info
Table of Contents
-
Challenge
Introduction to Exception Handling in Java
In this lab you will get experience with handling exceptions in Java. Simply put, exceptions are errors that are thrown within Java applications. When handling exceptions you can tell your code to perform certain actions if a specific block of code produces an error. Think of this pseudocode:
Try { This block of code } Catch (if an error occurs) { Perform this action }
This is the essence of exception handling in Java. This is called a
try
andcatch
block. Thetry
block is the code you want to run and thecatch
portion is what will be done if an error is produced in thetry
block. Additionally there is a third block called thefinally
block. This block will run code regardless of whether an exception is caught or not. This pseudocode for that would look like this:Try { This block of code } Catch (if an error occurs) { Perform this action } Finally (No matter what){ Perform this action }
Through this lab you will be using a booking application that asks you to pick a time for an appointment and books you in if you select an available time. Without the use of try and catch blocks any exceptions produced in this application would cause it to crash. You will make this application more robust by adding
try
,catch
andfinally
blocks that will handle common errors that will be produced throughout this application. Allowing it to run until completion without crashing.info > Note: Should you get stuck, there is a solutions folder located in
workspace/Solutions
that contains the full code solution for each task in this lab! -
Challenge
Creating your first try and catch blocks for handling exceptions
To begin, you will see what it looks like when an error is thrown with an exception. You will be using a booking application that asks you to pick a time for an appointment and books you if you select an available time. Follow the steps below to see how this works: Notice that the application crashes with an error code because an error was produced and the application doesn't know how to handle it. In the next section you will add a try and catch block to prevent this from happening and direct the application on what to do if a non integer is supplied.
The problem that caused the error in the first version of the script was this portion here:
while (true) { System.out.print("Enter the hour you want to book (9–17) or 0 to exit: "); int time = scanner.nextInt(); // if user types a string → InputMismatchException (no catch) }
This tells the application to save an Integer to the variable time. However, if a string is provided instead of the expected integer that will produce a
InputMismatchException
. Without a catch block the script won't know what to do with this and it will just fail out. You will fix this by replacing this portion of the code with the proper try and catch block. Follow the steps below to complete the challenge: -
Challenge
Using checked and unchecked custom exceptions
In Java there are built in exceptions, which are errors that Java will detect and produce under certain conditions. Additionally, there are custom exceptions that you can create, if you want to detect certain conditions that do not fit what you are looking for. In the following tasks you will edit two different scripts. One to enable a built in exception and the other to create a custom exception. In this first task you will use a built in exception called
InputMismatchException
. This raises an exception when the excepted input type doesn't match the input type provided. Next, you are going to get experience creating a custom exception calledInvalidTimeException
. This exception will check if the number provided is within the expected range and if it's not it will exit the program with a custom message that tells the user exactly what the mistake is. Now that you have experience with custom exceptions you will get experience with checked and unchecked exceptions. Checked exceptions throw errors when the java file is compiled while unchecked exceptions only through exceptions during runtime. Follow the steps below to complete this challenge: -
Challenge
Tracing exception origins and observing exception propagation
In this challenge, you will work with a script to understand how exception tracing and exception propagation work. This involves using the error messages produced by a script to identify where the original error originates.
This is important because, in a real-world situation, you may be working with programs that contain hundreds, if not thousands, of lines of code. Having the ability to locate exactly where an error has occurred is a valuable way to save time. Complete the following steps to finish the challenge:
-
Challenge
Applying consistent exception handling throughout an application
In this challenge you will complete the final version of this application by adding multiple catch blocks. This will ensure no matter what error is created by the user the appropriate warning message will be displayed.
The goal of this challenge is to create an application that can handle every type of potential error when accepting user input. This will include users providing incorrect input types, selecting a booked time slot and even a general catch block that can handle any type of non specific exception that occurs.
-
Challenge
Conclusion and Next Steps
Congratulations! You have completed this guided lab on Exception Handling in Java. By doing so you've learned how to properly handle exceptions that may be thrown in an application and resolve them without the application crashing.
You've also learned how to create custom exceptions to handle application specific issues. This way you can manage things like user input and ensure that the user provides only the specified type of input for the application.
Now you should consider improving the booking application to account for multiple days. Also, make an exception for if someone provides a day or time outside of work hours such as Saturday or 7pm to make the application more realistic. Keep exploring Java courses and labs to continue improving your coding skills!
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.