In this tutorial, we will learn about the Exceptions in Java. How to use them with the help of examples.

Define Exceptions in Java

Exception is the pre-defined class provide by java for trapping run-time or logical error. Exception can handle all type of logical errors. To determine the identity of the logical error, exception provides various sub-exception classes.

Types of Exceptions

There are two types of Exceptions.

  • Pre-define exception
  • user-define exception

Pre-define exceptions Method

ArithmeticException :-

This exception occurs when a number divided by 0 (divide by zero).

IOException :- This occurs when there is input or output error at run-time.

arrayIndexOutOfBoundsException :- It occurs when user accessing a wrong index position of array which is not exist.

StringIndexOutOfBoundsexception :- It occurs when user tries to access bad index position character from string.

NullPointerException :- NullPointerException occurs when user tries to access data/method by using object having null reference.

NumberFormateException :– This occurs when user tries to store a wrong format value in the variable / object and convert it into other type.

FileNotFoundException :– This Exception occurs when user tries to access the file for reading or writing which is not exist in the disk.

SecurityException:- Thisexception occurswhen java applet program tries to access the website which is restricted by the browser.

StackOverFlowException :- This Occurs when system trues to allocate memory (stack) for inner process but there is no sufficient memory.

InputMistmatchException :- This occurs when we try to store a wrong type value in array or variable or object.

OutOfMemoryExcption:- Occurs when there is not sufficient memory for new object of the class.

Example

import java.io.DataInputStream;
import java.io.IOException;

public class Errorm {
    public static void main (String args[])
    {
      DataInputStream z= new DataInputStream(System.in);  
      int x,y;
      try
      {
        System.out.println("Enter two number: ");
        x=Integer.parseInt(z.readLine());
        y=Integer.parseInt(z.readLine());
        System.out.println("Div="+(x/y));
      }
      catch(ArithmeticException e)
    
    {
        System.out.println(e.getMessage());
    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }
}    
}
Example of Exceptions

Let’s run the code

Input Value

Output Value

Output Value

User defined exception

User defined exception is the exception creates and use by the user in the program as per their own requirement.

Syntax

Class <name> extends Exception
 {
Public ClassName (String msg)
{
Super(msg);
}
 }
}
Syntax of User defined exception

According to Nature, Exception Are Two Types

Checked Exception

Checked exception are those exception caught by complier because user does not provide exception handling features in program.

Unchecked Exception

Unchecked exception are those exception caught and handle during execution of the program because programmer uses try……catch block within program.

Example

public class Errorm {
    public static void main (String args[])
    {
        String s= "sun";
        try
        {
            System.out.println(s.charAt(4));
        }
        catch (StringIndexOutOfBoundsException e)
        {
            System.out.println("bad index"+e.getMessage());
        }
    }
}
Example of Exceptions

Let’s run the code

Input Value

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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