Types of exceptions in java

What are the types of exceptions in java?

Do you know the types of exceptions in Java? Have you ever thought what can be the types of exceptions in Java? let us gain some knowledge about the available types of exceptions in Java.

But before we start making a list of the available types of exceptions in Java, let us first find out the need to know this topic. Why is it necessary to know the types of exceptions in Java? What is the purpose to know common exceptions in Java?

Let us try to assume the need by taking a real-life example.

In our daily life, we make different exceptions or faults. A brilliant student can get a low mark on his exam. That student can’t behave well with his friends. Like these, there are many exceptions or faults we make.

But this is not a good practice to make those faults. That is why we get to know the types of exceptions. Some exceptions can relate to the educational field. Some exceptions can relate to the behavior of an individual.

The types of exceptions help to find the reason behind those exceptions. That is why we need to categorize the exceptions into some groups.

The same thing goes for the common exceptions in Java. Different types of exceptions in Java help to find out the category of the exception. This will help to find out the solution for the exception.

 

Types of exceptions in java

What Is Exception In Java? Read Below

 

Exceptions are the most commonly used term in the Java programming language. In the Java programming language, we need to perform some tasks which can’t be performed by the simple approach. They are the exceptions. The process by which we manage the exceptions in Java is known as Exception Handling. We used two important methods Try-Catch for resolving such issues.

Exceptions are those things that occurred in the programming. The programmer knows well that there might be an exception in the program. For overcoming the exception, they need to implement a special approach. Like if in the program, there is a need to divide a number by zero. Now, dividing a number with zero is an exception. So, the programmer will try to write the code differently. In that approach, it can overcome the exception. This will help to run the program uninterruptedly.

 

 

What Are The Different Types Of Exceptions In Java?

 

The types of exceptions in Java can be divided into two main categories. Those categories can further be divided into many more categories. Depending upon the exception, we can divide the exceptions into two types:

  • Build In Exceptions
  • Compile Time Exception or Checked Exception: These are the types of exceptions in Java that are considered while compiling a piece of code. When a code is being compiled then they are checked by the compiler. Thus this type of exception can stop the running of the code.
  • Run Time Exception or Un Checked Exception: These are the types of exceptions in Java which can pass the compile time checking. They get stuck at the running time of any code. Then we need to solve the problem there. Then the code will become a free-form exception.
  • User Defined Exception

The Compile Time Exception can further be divided into six types of exceptions in Java. Also, the Run Time Exception can be further divided into six types of exceptions in Java.

Exceptions in Java with example will help to understand the concept in a better way. Let us try to find the details of the types of exceptions one by one briefly. That will help to get the topic.

 

Types Of Exceptions In Java – Build In Exception:

 

Build-in exceptions are those types of exceptions that are widely found in Java programming languages. This type can be divided into two parts. One part is checked in the compile time & another is checked at the run time. Some exceptions need to be solved before compiling the code. Else it will create a problem while running the piece of code.

The built-in exceptions are those exceptions that can be solved with the help of Java libraries. As they are the most common exceptions, their solution is also simple. Let us find out the list of exceptions.

  • Arithmetic Exception (Unchecked Exception): These are the exceptions related to the arithmetic problems in the Java program. Dividing a number with zero will create an exception. To solve this type of exception problem, we need to take the help of a certain library.
  • ClassNotFound Exception (Checked Exception): This is a special exception. It rises when we try to find a class that is not present in the programming. Several times we need to use this exception to run the code without a problem.
  • FileNotFound Exception (Checked Exception): This is the exception when we try to handle a file. Sometimes file can’t open or sometimes it can’t be accessible. In those cases, we need to take the help of this library function.
  • IO Exception (Checked Exception): It is the exception that arises with the input-output problem in the program. This is a very common problem in the Java programming language.
  • ArrayIndexOutOfBounds Exception (Unchecked Exception): This is the exception when we try to access the index of the array which is beyond the limit. We try to catch the certain array index out of the size. Then this exception is prompted out.
  • NoSuchMethod Exception (Checked Exception): This is the exception when we try to find out the special method. but those methods are not present in the program.
  • NoSuchField Exception (Unchecked Exception): It is the exception when we try to access the special field in any class. But the file is not present there. Then we use this library function.
  • NullPointer Exception (Unchecked Exception): This is the exception when we try to point out a null field. Then we need to use this library function to solve out.
  • IllegalArgument Exception (Unchecked Exception): This is the exception that will trigger when we try to send an illegal argument to any particular method.
  • IllegalState Exception (Unchecked Exception): This is the exception when the method is being called by an illegal part of the code.

It will be very easy to learn them with examples. Exceptions in Java with examples will help you in this case.

But it is a really difficult task to provide examples of every type of exception in Java in this article. But we have made those examples ready for you. If you needed, you can feel free to check those.

 

Types Of Exceptions In Java – User Defined Exception:

 

This type of exception can be visualized in some cases. In this type of case, the programmer himself develops an exception to easily solve a problem.

Here, we have created a try-catch block inside the main function. In the try-catch block, we will throw the argument to the specific function.

There the argument will be processed. Based on the operation it will return the string value.

The string value will then catch by the main function. Then it will print the message. We can implement this in a different approach. But to know exceptions in Java with example, we make this implementation.

This will help to understand the common exceptions in Java. It also helps to get the types of exceptions in Java in a good manner.

Example:

import java.util.*;  

public class Main{  

    public static void main(String args[]){ 

// Try Catch Block For Exception

         try{  

             throw new coding(12);}  

         catch(coding zap){  

             System.out.println(zap) ;  } } }  

class coding extends Exception{  

int a;  

     // Assigning The Value

     coding(int b) {  

         a=b;  }  

     // Returning The Message

     public String toString(){  

         return ("Value:  "+a);  } 

Let us find out the output of the above code. This will help to understand the types of exceptions in Java.

Output:

Types of Exceptions in java output

 

Conclusion:

 

As we saw types of exceptions in Java are very important.

Common exceptions in Java will be used in the future for implementing large applications. So, we have to remember it.

Different types of exceptions in Java help to understand the difference among them in a better way. Exceptions in Java with example will help you a lot in this case.

So, hope you have liked this piece of article. Share your thoughts in the comments section and let us know if we can improve more.

Also, if you guys are looking to get help with Java Assignments or Programming then you can use our Java Homework Help Services.

Leave a Comment

Your email address will not be published. Required fields are marked *