Java Exceptions List 🎯

beginner
13 min

Java Exceptions List 🎯

Welcome to our comprehensive guide on Java Exceptions! This tutorial is designed to help both beginners and intermediates understand and work with Java exceptions.

What are Exceptions in Java? 📝

Exceptions are events that occur during the execution of a program that disrupt the normal flow of instructions. They are used to handle errors and exceptions that might occur while running a Java program.

Types of Exceptions in Java 📝

Java has two types of exceptions:

  1. Checked Exceptions
  2. Unchecked Exceptions

Checked Exceptions 📝

Checked exceptions are those exceptions that are checked by the compiler at compile-time. These exceptions are subclasses of Exception class. Examples include IOException and SQLException.

Unchecked Exceptions 📝

Unchecked exceptions are those exceptions that are not checked by the compiler at compile-time. These exceptions are subclasses of RuntimeException class. Examples include NullPointerException, ArithmeticException, and ArrayIndexOutOfBoundsException.

Handling Exceptions in Java 📝

Java provides three ways to handle exceptions:

  1. Try-Catch Block
  2. Try-Catch-Finally Block
  3. Throw and Throws Keywords

Try-Catch Block 💡

A try-catch block is used to catch exceptions and handle them appropriately. The try block encloses the code that might throw an exception, and the catch block follows it and contains the code to handle the exception.

Here's a simple example:

java
try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero!"); }

In the above example, we are dividing by zero which throws an ArithmeticException. The try block encloses the code that might throw an exception, and the catch block catches the ArithmeticException and handles it by printing "Cannot divide by zero!"

Try-Catch-Finally Block 💡

A try-catch-finally block is similar to a try-catch block, but it also includes a finally block. The finally block contains the code that is always executed, whether an exception occurs or not.

Here's an example:

java
try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero!"); } finally { System.out.println("This code will always run!"); }

In the above example, the finally block prints "This code will always run!" regardless of whether an ArithmeticException is thrown or not.

Throw and Throws Keywords 💡

The throw keyword is used to throw an exception manually, and the throws keyword is used to declare that a method might throw an exception.

Here's an example:

java
public class ExceptionExample { public static void main(String[] args) { myMethod(); } public static void myMethod() throws ArithmeticException { throw new ArithmeticException("Division by zero exception!"); } }

In the above example, we are manually throwing an ArithmeticException in the myMethod() function, and the throws keyword is used to declare that this method might throw an ArithmeticException.

Quiz 🎯

Quick Quiz
Question 1 of 1

Which of the following is an example of an Unchecked Exception in Java?

That's all for our Java Exceptions List tutorial! We hope you found it helpful and informative. Happy coding! 😄