in , ,

Java Exceptions

Java Exceptions
Java Exceptions

Java Exceptions

In this tutorial, we will find out about exemptions in Java. We will cover errors, exemptions, and various types of exceptions in Java.

An exception is an unexpected function that happens during program execution. It influences the progression of the program guidelines which can make the program end anomalous.

An exemption can happen for some reasons. Some of them are:

  • Invalid user input
  • Device failure
  • Loss of network connection
  • Physical limitations (out of disk memory)
  • Code errors
  • Opening an unavailable file

Java Exception hierarchy

Here is a simplified diagram of the exception hierarchy in Java.

As you can see from the image above, the Throwable class is the root class in the hierarchy.

Note that the hierarchy splits into two branches: Error and Exception.


Errors

Errors represent irrecoverable conditions, for example, Java virtual machine (JVM) running out of memory, memory spills, stack flood mistakes, library incongruence, interminable recursion, and so on

Errors are generally outside the ability to control the programmer and we ought to make an effort not to handle errors.


Exemptions

Exemptions can be gotten and taken care of by the program.

At the point when an exemption happens inside a strategy, it makes an object. This object is called the exception object.

It contains data about the special case, for example, the name and description of the exemption and condition of the program when the exemption happened.

We will learn to handle these exemptions in the next tutorial. In this tutorial, we will currently zero in on various types of exemptions in Java.


Java Exception Types

The exception hierarchy also has two branches: RuntimeException and IOException.


1. RuntimeException

A runtime exception happens due to a programming error. They are also known as unchecked exceptions.

These exceptions are not checked at compile-time but run-time. Some of the common runtime exceptions are:

  • Improper use of an API – IllegalArgumentException
  • Null pointer access (missing the initialization of a variable) – NullPointerException
  • Out-of-bounds array access – ArrayIndexOutOfBoundsException
  • Dividing a number by 0 – ArithmeticException
  • You can think about it in this way. “If it is a runtime exception, it is your fault”.

The NullPointerException would not have occurred if you had checked whether the variable was initialized or not before using it.

An ArrayIndexOutOfBoundsException would not have occurred if you tested the array index against the array bounds.


2. IOException

An IOException is also known as a checked exception. They are checked by the compiler at the compile-time and the programmer is prompted to handle these exceptions.

Some of the examples of checked exceptions are:

Trying to open a file that doesn’t exist results in FileNotFoundException
Trying to read past the end of a file

Now we know about exceptions, we will learn about handling exceptions in the next tutorial.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Reflection

Java Reflection

Java Exception Handling

Java Exception Handling