Table of Contents
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
- unchecked Exception – Unchecked Exception are not checked at compile time . They are checked runtime
Hierarchy of Java Exception classes
We can use five keywords in exception handling in Java
- try block
- catch block
- finally block
- throw
- throws
Type of Exceptions in java
- Arithmetic Exeption
- Classnotfound Exeption
- Illegal Argument Exeption
- IndexoutofBounds Exeption
- Inputmismatch Exeption
- IO Exeption
}
How to Handle Arithmetic Exception in Java
Program 1
Program 2
Program 3
How to handle array index out of bound of exception in Java
Program 1
Program 2
Program 3
Related Links
Abstract class in C++ Abstract class in Java
Encapsulation in C++ Encapsulation in Java