Ausnahmebehandlungstechniken 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

Ausnahmebehandlungstechniken in Java
Java-Ausnahmebehandlung?
[Java] Ausnahmebehandlung
Informationen zur Behandlung von Java-Ausnahmen
Behandlung von Java-Ausnahmen
Fragen in Java-Ausnahmebehandlung werfen und versuchen-fangen
[Java] Praxis der Ausnahmebehandlung [Ausnahme]
[Java] Informationen zur Behandlung von Try-Catch-Ausnahmen
[Ruby] Ausnahmebehandlung in Funktionen
Ausnahmebehandlung
Ausnahmebehandlung Ausnahme
[Inhouse-Studiensitzung] Behandlung von Java-Ausnahmen (26.04.2017)
Schrittweises Verständnis der Behandlung von Java-Ausnahmen
[Für Java-Anfänger] Informationen zur Ausnahmebehandlung
Informationen zur Ausnahmebehandlung
Informationen zur Ausnahmebehandlung
Behandlung von Ruby-Ausnahmen
Partisierung in Java
[Java] Ausnahmeinstanz
Janken in Java
Ruby-Ausnahmebehandlung
Java (Ausnahmebehandlung, Threading, Sammlung, Datei-E / A)
Umfangsrate in Java
FizzBuzz in Java
Nutzen Sie entweder für die individuelle Ausnahmebehandlung in der Java Stream-API
try-catch-finally Ausnahmebehandlung Verwendung von Java
[Java] Behandlung von Java Beans in der Methodenkette
Lesen Sie JSON in Java
Interpreter-Implementierung durch Java
Machen Sie einen Blackjack mit Java
Janken App in Java
Einschränkungsprogrammierung in Java
Setzen Sie Java8 in Centos7
Praxis der Ausnahmebehandlung (ArithmeticException)
NVL-artiger Typ in Java
Verbinden Sie Arrays in Java
"Hallo Welt" in Java
Aufrufbare Schnittstelle in Java
[Java] Wirf eine Ausnahme aus
Azure funktioniert in Java
Spring Boot-Ausnahmebehandlung
Einfache HTML-Spezialchars in Java
Boyer-Moore-Implementierung in Java
Hallo Welt in Java
WebApi-Memorandum mit Java
Typbestimmung in Java
Befehle in Java ausführen (Ping)
Verschiedene Threads in Java
Implementierung der Heap-Sortierung (in Java)
ASCII-Kunst in Java
Listen in Java vergleichen
POST JSON in Java
Fehler in Java ausdrücken
Fehlerbehandlung mit Graphql-Ruby
Erstellen Sie JSON in Java
Datumsmanipulation in Java 8
Was ist neu in Java 8?
Verwenden Sie PreparedStatement in Java
Was ist neu in Java 9,10,11