Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
In this lecture, you will learn how to handle multiple exceptions within one single program.
We do that by working on a simple use case which is to read one command line argument and to parse it
into a integer value and display it. Let's create a new class. Right click, default, on the default package,
say New -> Class. Call it MultiCatch. Main method selection,
Finish.
Double click to maximize, control+d to delete.
So the very first thing we are going to do is to read the input from the command line arguments. String
input is equal to arg[0].
So we are assuming that one command line argument will be provided, args[0].
And that command line argument, the JVM puts it inside this string array. You already know that.
And we are reading it into a string. Let's display that. Sysout, within double quotes, Input is:
+input. Now let's parse it into integer. int output is equal to, Integer,
the wrapper class, dot parseInt(input). Sysout,
within double quotes, Output is:, +output. So far we haven't handled any exceptions.
Let's have some code after this block. Sysout, More code can go here.
So here there is a chance of a ArrayIndexOutOfBoundException. Let's try to run this program without providing
any inputs.
So you can create new configuration for MultiCatch.
So by clicking on the New button and if you don't provide any arguments by going to this
arguments tab, run it and we'll get a ArrayIndexOutOfBoundException because we are trying to access
index 0 and didn't provide any inputs. Let's handle it.
Surround it with try.
And here is where our code is ending, catch(). Close the try
and then the catch begins.
The first exception is ArrayIndexOutOfBoundException.
If you hit control+space, you will get that and you can name the object reference whatever
you want, eaf or simply e.
Open the catch block, sysout, within double quotes,
Input is required.
Or you can also say Enter one number.
Now let's run this program again.
Earlier our program termination was abrupt.
This code never got executed. Run it again.
Now atleast it says Input is required and the rest of the portion also executes. Now let's configure
a command line argument or pass in a command line arguments. Go to the run configurations.
Select MultiCatch, Arguments, provide abc. You are providing a value but it is not a number. Run it
and now the exception is, for input string abc, there is a NumberFormatException.
It prints the input.
So upto this point it is happy.
But then on this line, we get a NumberFormatException.
As a result since we are not handling it, this portion will never be executed.
That is what you see in the output here.
So let's handle that using multiple catch blocks. catch(NumberFormatException e).
You can name the object references with the same identity or give them the same name because each catch
block has its own scope and the variables are local to that particular catch block.
You can also name it different but we can have the same name e.
Sysout,
within the catch up,
we display a user friendly message saying Invalid input provided. Run it again.
Now input is abc. It says invalid input is provided and the code after the multiple catch blocks gets
executed. To run it successfully,
go here,
Run configurations -> Arguments, provide a valid value 123, Run.
Now everything works as expected.
Input is:123, Output is:123 and the More code can go here also gets executed. So to use multiple catches,
simply surround the code with try followed by as many catches as you want.
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team