Exception Handling in Java


 
Introduction

Exception handling is one of the most important and critical part in application development stage. Firstly, before we dig in to the deep of exception handling; firstly, let's have a brief idea of the difference between an Error and Exception. Basically both Exceptions and Errors are subclasses of Throwable class.

Error

An error indicates serious problems that our applications should not try to catch. Mostly these errors indicate a problem that mainly occurs due to lack of system resources and resource limitations. Errors mostly occur at runtime and which they belongs to the unchecked category.

Eg:- System Crash error, Out of memory error, StackOverflow error.

Note:-

StackOverflow Error happens when you execute too many methods one inside another (such as infinite recursion), which is limited by the size of the stack.

Out of Memory Error happens when the JVM runs out of space to allocate new objects, which are allocated on the heap.

Errors are also kind of unchecked exception and the programmers are not required of worrying handling those. It is a bad idea of trying to handle errors by using try-catch blocks for errors since recovery from an error is not possible and the program should allowed to be terminated. But it is required to deal with handling Runtime Exceptions though it is a kind of unchecked exception in code.

Exception

An Exception indicates conditions that our applications might want to catch. Exceptions are the problems which may occur at both compile time and runtime. It mainly occurs in the code written by the developers (developer is responsible for handling exceptions). Exceptions can be divided into two categories such as Checked Exceptions and Unchecked Exceptions.

Checked exceptions are required for programmers to handle and recover. It is a good idea to recover from such exceptions programatically like File Not Found Exception, Parse Exception etc using try-catch blocks or may throw them back to the caller.

Unchecked exceptions are kind of exceptions that may occur when something is not working in a proper usual manner like ArrayOutOfBoundException, ClassCastException etc. An unchecked exception (also known as runtime exception) is something that has gone wrong with the programming language and is unrecoverable. Just because this is not a compile time exception, it means that you don't need to handle it.

Some applications'll use try-catch or throws clause for Runtime Exceptions and their subclasses but from the language perspective it is not required to do so. Runtime exceptions represent problems that are a direct result of a programming problem, and as such shouldn't be caught since it can't be reasonably expected to recover from them or handle them.


Note:-

Recovery from a Runtime Exception is generally possible but the guys who designed the class/exception deemed it unnecessary for the end programmer to check for such exceptions.




Keywords:  java  java basics  java programming  spring framework  java spring  java code  java programming language  java language learn java  jdk  java jdk  java development kit 



Post a Comment

0 Comments