course content

Course Content

Introduction to Java

ExceptionsExceptions

Exceptions may sound intimidating, but they are essential to any programming language. They are a mechanism that allows your program to handle unexpected situations that may occur during the execution of your program. Think about it like a safety net for your code. It allows your program to continue running smoothly even if something goes wrong. This chapter will dive into exceptions and learn how to handle them like a pro.

Types of Exceptions

Java has two types of exceptions: checked exceptions and unchecked exceptions. Let's take a look at each one of them.

checked exception

Checked exceptions are the ones that the compiler checks for at compile-time. These are exceptions that extend the java.lang.Exception class or any of its subclasses, excluding the RuntimeException class and its subclasses. Examples of checked exceptions include IOException, SQLException, etc. For instance, when we are reading a file, if the file is not found, the compiler will check for that and throw a FileNotFoundException.

java

Main.java

unchecked exceptions

Unchecked exceptions are the ones the compiler does not check for at compile-time. These are exceptions that extend the java.lang.RuntimeException class or any of its subclasses. Examples of unchecked exceptions include NullPointerException, ArrayIndexOutOfBoundsException, etc. For instance, when trying to access an element of an array that doesn't exist, an ArrayIndexOutOfBoundsException will be thrown.

java

Main.java

Creating and Throwing Exceptions

In some cases, you might want to create your custom exception. You can create a new class that extends the java.lang.Exception or java.lang.RuntimeException class. For instance, you can create your custom exception class called NegativeNumberException and throw it when a negative number is passed as a parameter to a method.

java

Main.java

Handling Exceptions

To handle exceptions, we use the try-catch block. The try block contains the code that may throw an exception. The catch block contains the code executed when an exception is thrown.

java

Main.java

Managing multiple catch blocks

Now, we know that sometimes, there can be more than one error or exception in our code. And, we can handle each type of exception differently. This is where multiple catch blocks come in handy. Here's how you can handle multiple catch blocks:

java

Main.java

So, you can have multiple catch blocks, one after the other, each catching a different type of exception. It's important to remember that catch blocks should be ordered from the most specific to the most general. The first catch block that matches the type of exception thrown by the code inside the try block will handle the exception, and the others will be ignored. The program will terminate if the exception type is not matched in any of the catch blocks. Here's an example to explain better:

java

Main.java

Finally block

The "finally" block is optional, but it is useful when we want to perform some cleanup operations, regardless of whether an exception was thrown. For example, we can close a file in the "finally" block, as shown below:

java

Main.java

That's it! Now you know how to handle exceptions in Java. If you have any questions, feel free to ask. Let's move on to the next lesson.

1. What is the purpose of using multiple catch blocks in Java Exception handling?
2. What is the purpose of the finally block in Java exception handling?

question-icon

What is the purpose of using multiple catch blocks in Java Exception handling?

Select the correct answer

question-icon

What is the purpose of the finally block in Java exception handling?

Select the correct answer

Section 4.

Chapter 2