Exception Handling using try catch and finally statement in java using netbeans

Exception Handling try-catch and finally in Java

Exception Handling using try-catch and finally statement in java using NetBeans- In this tutorial, I am going to share with you a technique for handling conditions where the program crashes or errors when running in the Java programming language, the technique is called  Exception Handling with try-catch, and finally.

What is an Exception?

An exception is a flow of events that runs a process in the program, the event can be an error or error in the program that we make, the error can occur due to several factors, including:

input errors, the type of data format entered is wrong, the use of arrays that exceeds the limit, Etc.

There are many types of exceptions that we can catch using the try-catch function, one of which is an ArrayIndexOutOfBoundsException , this exception indicates that the number of arrays we input exceeds the limit, and the message will appear when the application is run which will cause the program to stop.

Try-catch statements

How do we find out what type of error or exception appears, in the following example, we will create a simple program, where the program is intentionally made into an error or an error occurs.

Example: program without try-catch exception:


try catch Exception

The type of exception that appears in the program is  ArithmeticException, the error occurs because there is a division of 0 (zero), if this happens, the user will be confused by the error and cause a force close. To solve this problem, we can use a try-catch statement, as in the following example:

Example: program with try-catch exception:

Every statement that can cause an exception must be in a try because to handle where an error occurs that you want to process, catch is used to handle the type of exception, and the error that is raised will be treated as an object.


try catch Exception

Example: how to use catch array exception in java using NetBeans:

For the second example, we will create an array variable with a predetermined amount of data, the program will catch and print an exception if we call a value outside the array limit.


try catch Exception

Multiple catch exceptions in java:

In the Java programming language, we can use more than one catch, to catch different types of exceptions in the statements in the try.

In the following example, we will combine the two programs that we previously created into one, in that program we will catch 2 different types of exceptions, namely ArithmeticException and ArrayIndexOutOfBoundsException.

Example: how to use multiple catch exceptions in java using NetBeans:


try catch Exception

The program will first check the ArrayIndexOutOfBoundsException catch to see if this type of exception is found in the try, if so, it will issue a statement in it, and if not, it will check the second catch, which is  ArithmeticException .

So the program will not issue output on both catches simultaneously, even though 2 types of errors are found in the try, the type of exception at the top will be displayed first.

Finally statement exception:

The finally statement is used to execute program code if an exception occurs or no exception occurs, so the block of code in it will continue to be executed under any conditions.

Example: how to use the finally statement in exception handling in java:

The result will be like this

try catch Exception

That’s the tutorial that I’m sharing this time, I’m sorry if I made a mistake, if there’s anything you want to ask, please comment below.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button