Techniques de gestion des exceptions en 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

Techniques de gestion des exceptions en Java
Gestion des exceptions Java?
[Java] Gestion des exceptions
À propos de la gestion des exceptions Java
Gestion des exceptions Java
Questions sur la gestion des exceptions Java throw et try-catch
[Java] Pratique de la gestion des exceptions [Exception]
[Java] À propos de la gestion des exceptions try-catch
[Ruby] Gestion des exceptions dans les fonctions
Gestion des exceptions
Exception de traitement des exceptions
[Session d'étude interne] Gestion des exceptions Java (2017/04/26)
Compréhension étape par étape de la gestion des exceptions Java
[Pour les débutants en Java] À propos de la gestion des exceptions
À propos de la gestion des exceptions
À propos de la gestion des exceptions
gestion des exceptions ruby
Partition en Java
[Java] Instance d'exception
Janken à Java
Gestion des exceptions Ruby
Java (gestion des exceptions, threading, collection, fichier IO)
Taux circonférentiel à Java
FizzBuzz en Java
Tirez parti de l'un ou l'autre pour la gestion des exceptions individuelles dans l'API Java Stream
gestion des exceptions try-catch-finally Comment utiliser java
[Java] Gestion des Java Beans dans la chaîne de méthodes
Lire JSON en Java
Implémentation de l'interpréteur par Java
Faites un blackjack avec Java
Application Janken en Java
Programmation par contraintes en Java
Mettez java8 dans centos7
Pratique de gestion des exceptions (ArithmeticException)
NVL-ish guy en Java
Joindre des tableaux en Java
"Hello World" en Java
Interface appelable en Java
[java] Lancer une exception
Fonctions Azure en Java
Gestion des exceptions Spring Boot
Simple htmlspecialchars en Java
Implémentation Boyer-Moore en Java
Hello World en Java
Mémorandum WebApi avec Java
Détermination de type en Java
Exécuter des commandes en Java (ping)
Divers threads en java
Implémentation du tri de tas (en java)
Art ASCII à Java
Comparer des listes en Java
POST JSON en Java
Exprimer l'échec en Java
Gestion des erreurs avec Graphql-ruby
Créer JSON en Java
Manipulation de la date dans Java 8
Nouveautés de Java 8
Utiliser PreparedStatement en Java
Nouveautés de Java 9,10,11