Java -Exception-hierarchy-types-example

An exception is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.
The Exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.


Exception Hierarchy

All exception and errors types are sub-classes of class Throwable. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. IndexOutOfBoundsException is an example of such an exception. Another branch, Error is used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself(JRE). OutOfMemoryError is an example of such an error.




We have three categories of Exceptions

Checked exceptions  

A checked exception is an exception that is checked (notified) by the compiler at compilation time, these are also called compile-time exceptions. These exceptions cannot simply be ignored, the programmer should take care of (handle) these exceptions.

Example:

import java.io.*;
class Main {
public static void main(String[] args) {
FileReader file = new FileReader("D:\\test\\knf.txt");
BufferedReader fileInput = new BufferedReader(file);
// Print first 9 lines of file "D:\test\knf.txt"
for (int counter = 0; counter < 9; counter++)
System.out.println(fileInput.readLine());
fileInput.close();
}
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
        at Main.main(Main.java:5)


Unchecked exceptions

An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.

Example:


class Main {
public static void main(String[] args) {
int a = 1;
int b = 0;
int c = a / b;
System.out.println(c);
}
}


Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.main(Main.java:6)


Errors 

These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.


Exceptions Methods


Method & Description

public String getMessage():
Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.

public Throwable getCause():
Returns the cause of the exception as represented by a Throwable object.

public String toString():
Returns the name of the class concatenated with the result of getMessage().

public void printStackTrace():
Prints the result of toString() along with the stack trace to System.err, the error output stream.

public StackTraceElement [] getStackTrace():
Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack.

public Throwable fillInStackTrace():
Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete