Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Exception Handling in Java language

Posted on February 20, 2017January 16, 2020

Table of Contents

  • Exception Handling in Java language
    • What is an exception? An exception is one of the abnormal conditions of an unwanted situation or unexpected events that occur during the execution of a program. An exception may lead to termination of the program if not handled properly.
      • Hierarchy of Java Exception classes
      • Type of Exceptions in java

Exception Handling in Java language

In this tutorial, we will discuss the concept of Exception handling in Java language.

Exception handling is one of the important, most useful and powerfull mechanism to handle the runtime error in Java. We will discuss the five keywords for exception handling in Java.

What is an exception?

An exception is one of the abnormal conditions of an unwanted situation or unexpected events that occur during the execution of a program. An exception may lead to termination of the program if not handled properly.

When can an exception occur?

An exception can occur twice:

  • Run time exception – it is called runtime exception
  • Compile-time exception – it is called compile-time exception

Java exceptions are basically java objects.

Unlike errors, exceptions can be used to prevent the program from terminating automatically.

Exceptions can be handled using the try and catch blocks.

Error
An error is thrown by the java runtime system and indicates that some irrecoverable condition has occured during execution of the program.

Different between exceptions and errors

An exception can be handled as a user or programmer but  we can not handle the  Error by user or programmer

What type of exception occur?

Two type of exception occur

  • Checked Exeption – Checked Exeption are checked compile time
Eg -IO Exeption, SQL Exception,
  • unchecked Exception – Unchecked Exception are not checked at compile time . They are checked runtime
Eg – Arithmetic Exception, NullPointerException,ArrayIndexOutOfBoundsException

 

Hierarchy of Java Exception classes

 

We can use five keywords in exception handling in Java

  1. try block
  2. catch block
  3. finally block
  4. throw
  5. throws
The codes which might throw some exceptions should be kept in try block.
The catch block can have a code to handle the exception or log.
Finally, block can be used to clean up code or release some resources that are utilized in the program.

Type of Exceptions in java

  1. Arithmetic Exeption
  2. Classnotfound Exeption
  3. Illegal Argument Exeption
  4. IndexoutofBounds Exeption
  5. Inputmismatch Exeption
  6. IO Exeption
syntex:
try
{//block of code
}catch(Exeption e){
//block of code
}finally{
//block of code

}

How to Handle Arithmetic Exception in Java

Program 1

Exception Handling in Java language
Example

Program 2

Exception Handling in Java language
Example

Program 3

Exception Handling in Java language
Example

How to handle array index out of bound of exception in Java

Program 1

Exception Handling in Java language
Example

Program 2

Exception Handling in Java language
Example

Program 3

Hierarchy of Java Exception classes
Example

 

Exception Handling in Java language
Example
Throw exception
Exception Handling in Java language
Example

Related Links

Abstract class in C++                                           Abstract class in Java

Encapsulation in C++                                       Encapsulation in Java

Polymorphism in C++                                      Methodoverriding in Java

Method overloading in java                             Constructor overloading in Java

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes