All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Types Of Exception In Java

Last Updated : Mar 11, 2024

Types Of Exception In Java

In this article we will show you the solution of types of exception in java, Java defines a number of exception kinds that are connected to its different class libraries.

Let’s discuss below about the type of exceptions in Java and also check out the examples.

Step By Step Guide On Types Of Exception In Java :-

Built-in Exceptions -

The exceptions that are present in Java libraries are known as built-in exceptions. These exceptions can be used to explain a few different fault scenarios.

Arithmetic Exceptions -

When an extraordinary circumstance occurs during an arithmetic operation, an ArithmeticException is raised.

ArrayIndexOutOfBoundsException -

An array has been accessed with an unlawful index, as shown by the throwing of the ArrayIndexOutOfBoundsException. Either the index is zero or it exceeds or is equal to the array's size.

ClassNotFoundException -

When attempting to access a class whose definition cannot be found, the ClassNotFoundException is triggered.

FileNotFoundException -

A file that cannot be opened or is not available will throw the

IO Exception -

When an input-output operation fails or is interrupted, the exception IOException is raised.

InterruptedException -

When a thread that is waiting, sleeping, or performing some processing is interrupted, InterruptedException is raised.

NoSuchFieldException -

When a class does not include the field (or variable) requested, NoSuchFieldException is thrown.

NoSuchMethodException -

When trying to access a method that is not there, the NoSuchMethodException exception is raised.

NullPointerException -

This error occurs when referring to a null object's members. Null stands for nothing.

NumberFormatException -

When a method fails to convert a string into a numeric format, a NumberFormatException is thrown.

IllegalArgumentException -

This exception is thrown when a method receives an argument that is inaccurately unfit for the relation or condition that has been specified. It fits into the unchecked exception category.

IllegalStateException -

When the method is not accessed for the specific operation in the programme, this exception will throw an error or error message. It fits into the unchecked exception category.

// Java program to demonstrate ArithmeticException
class ArithmeticException_Demo
{
public static void main(String args[])
{
try {
int a = 30, b = 0;
int c = a/b;
System.out.println ("Result = " + c);
}
catch(ArithmeticException e) {
System.out.println ("Can't divide a number by 0");
}
}}
//Java program to demonstrate NullPointerException
class NullPointer_Demo
{
public static void main(String args[])
{
try {
String a = null; //null value
System.out.println(a.charAt(0));
} catch(NullPointerException e) {
System.out.println("NullPointerException..");
}
}
}
// Java program to demonstrate StringIndexOutOfBoundsException
class StringIndexOutOfBound_Demo
{
public static void main(String args[])
{
try {
String a = "This is like leo ";
char c = a.charAt(24); // accessing 25th element
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e) {
System.out.println("StringIndexOutOfBoundsException");
}
}
}
  1. We have declared two integer variables a &b.
  2. The result will be stored in c.
  3. Will use catch block and Print Can't divide a number by 0
  4. Run the code
  5. Creating class NullPointer_demo
  6. Will implement try block and declare string a=null
  7. Run the code
  8. Creating a string length which is 22
  9. Accessing the 25 number element
  10. Will use catch(StringIndexOutOfBoundsException e) and print above statement
  11. Run the above code

Conclusion :-

So, in this tutorial we learned about types of exception in Java. We discuss about all the sub types of built-in exceptions.

I hope this article on types of exception in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪