EXCEPTIONS IN JAVA

INTRODUCTION

java-logoAn exception is an error condition that is not detected at runtime, that is, during program execution. Such an error prevents the program from progressing properly and requires handling. If the exception is not handled, the program stops. We introduce the base classes from which all exceptions are derived. In Java all exceptions are represented by classes. See the figure.

Classi base

Throwable which is derived from Object, means to lift, is the base class of all exceptions. The difference between the two subclasses Exception and Error is that Exception is the superclass of all application-type exceptions that can be handled programmatically such as division by zero ,while Error is the superclass of exceptions that affect the JVM, when we have exceptions of this type there is not much we can do to recover the problem.

USE TRY/CATCH BLOCKS

With the try/catch block we identify areas within which error conditions could be generated. Let’s see how it works.

try, catch

In the try block we go to insert the various instructions that might cause an exception, try means try to execute the following instructions. If there are no errors all catch blocks are skipped and the program continues normally. Conversely, if an exception occurs it can be caught within one of the catch blocks and handled. We can insert different handlers based on the type of exception that occurred, the blocks will be considered from top to bottom. Inside the round brackets we need to specify an exception that comes from Throwable and a variable. If the class matches the exception that was raised, then it will be handled in that catch block. Let’s look at an example to clarify this.

Division ok
Division without error

In cases of division by zero we will have that the exception is caught and an informational message is shown, if we wish we can resume the program by informing the user that he has entered an invalid number and return with a return statement.

Division by zero

Within a catch block we can insert any class that is derived from Throwable, including Throwable itself. We modify the code and use the Exception class

Gerarchia

Exception catches all of its own subclasses; this could be good or bad depending on how precise we want to be in handling exception classes. We have to be careful not to put the Exception block first of all, otherwise if we have more specific exceptions that are further down, they will not be executed. The general rule is that more specific exceptions should be put in first, and finally the Exception block.

Class Exception

We see in the figure how we can catch exceptions by exploiting the Multi-catch.

Multi catch

EXCEPTIONS NOT CAPTURED

What happens if an exception is raised that we do not catch because we have not inserted a try/catch block? Let’s look at an example.

Division ok

If we cause a division by zero without try/catch blocks the current method is immediately dropped and we go up the top of the stack to see if there is a block that can handle the exception, in our case the code jumps into the main. If the exception is handled then the program continues otherwise it ends with an error in the main method. See the figure.

Exception

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB

EXECUTION OF THE EXAMPLE CODE

  • Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.

        java -jar –enable-preview CorsoJava.jar

  • Or run the main found in the file CorsoJava.java.