Exception handling techniques in Java

Exception handling in Java is made possible through the use of some keywords like try, catch, throw, throws, and finally. These keywords are used to manage how exceptions are thrown and handled.

java learninig.png

Any piece of code that might cause an exception to be thrown is written in a try block. Code that might throw an exception usually deal with input values, which are not guaranteed to always be the way the programmer wants.

Imagine a baby that tries to walk. You simply put your hands around the baby to ensure that the baby does not fall and injure themselves. In the same way, the try block is used to surround code that might throw an exception while running.

A try block is followed immediately by a catch block or a finally block or both.

A catch block does exactly what its name says: it catches an exception thrown in the try block. Since a number of exceptions can be thrown, the catch block must specify the class of exception it is handling.

Beyond a catch block, there is the finally block, which simply works when the try block is done. So, the finally block waits for the try block to execute. Note that a try block can be followed by a catch block or a finally block or a combination of both. If the try block has a catch block, finally runs after the catch, otherwise the finally block runs immediately after the try block.

So imagine the finally block as the final resort for a try block. The finally block is normally used for handling resources that might not have been properly utilized by the try block.

A method, or a piece of code that performs a specific function in Java, can throw a type of exception by using the throws keyword in the method heading.

Exceptions in the Error or RuntimeException and their subclasses need not be included in the throws statement. They are classified as Unchecked Exceptions, which are exceptions that should be prevented in any way possible and must not be consciously allowed to occur.

The number of Unchecked Exceptions that can occur in a program are so enormous that we cannot throw all the Unchecked Exceptions in a method. It would cause the method to lose its clarity, so Java assumes that a programmer running a program does not intentionally throw these type of exceptions.

Every method is already liable of throwing unchecked exceptions when something goes wrong, so no need to add unchecked exceptions in the throws statement.

The throw keyword is used to specifically throw an exception in a method. It simply serves the normal use as in the verb throw: it throws an object of the Throwable Class. You cannot throw objects that are not in the Throwable Class or any of its subclasses. Be careful to not throw Unchecked Exceptions with the throw keyword. Use the throw keyword for throwing Checked Exceptions.

Java course in chennai

Recommended Posts

Exception handling techniques in Java
Java exception handling?
[Java] Exception handling
Java exception handling
Java exception handling
Questions in java exception handling throw and try-catch
[Java] Practice of exception handling [Exception]
[Java] About try-catch exception handling
[Ruby] Exception handling in functions
Exception handling
Exception handling Exception
[In-house study session] Java exception handling (2017/04/26)
Step-by-step understanding of Java exception handling
[For Java beginners] About exception handling
About exception handling
About exception handling
ruby exception handling
Partization in Java
[Java] Exception instance
Rock-paper-scissors in Java
Ruby exception handling
Java (exception handling, threading, collection, file IO)
Pi in Java
FizzBuzz in Java
Leverage Either for individual exception handling in the Java Stream API
try-catch-finally exception handling How to use java
[Java] Handling of JavaBeans in the method chain
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
Exception handling practice (ArithmeticException)
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
[Ruby] Exception handling basics
[java] throw an exception
Azure functions in java
Spring Boot exception handling
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Error handling in Graphql-ruby
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11