Chapter 4: Exception Handling
In Chapter 1, Getting Started with Java 17, we briefly introduced exceptions. In this chapter, we will treat this topic more systematically. There are two kinds of exceptions in Java: checked and unchecked. We’ll demonstrate each of them, and the differences between the two will be explained. Additionally, you will learn about the syntax of the Java constructs related to exception handling and the best practices to address (that is, handle) those exceptions. The chapter will end on the related topic of assertion statements, which can be used to debug the code in production.
In this chapter, we will cover the following topics:
- The Java exceptions framework
- Checked and unchecked (runtime) exceptions
- The
try
,catch
, andfinally
blocks - The
throws
statement - The
throw
statement - The
assert
statement - Best practices of exception handling
So, let’s begin!