NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Now that you know the keywords and the syntax to handle exceptions, it's time we handle those exceptions

which we have been creating in earlier lectures. So go to the Eclipse IDE, go to the very first class which

created an exception which is the Division.java. And we need to handle the ArithmeticException.

Right click, Run As -> Java application.

It asks to enter two integers.

So let's enter 19 and then a 0 and then we get a ArithmeticException,

division by zero.

So the code that is causing the exception should going into a try catch. This is the code we want to execute

even if a exception is thrown.

So let's surround this entire code within a try, flower bracket and that ends here. It should follow with a catch

and a exception. In our case that exception is ArithmeticException. Arith, control+space and

Eclipse will help us out.

Let's call it e.

That is a normal convention that we do follow in real time applications.

In here we can do a sysout,

Please do not enter a 0. Or you can say division by zero has occurred or any other user friendly message.

Let's run it again and see what happens this time.

So when we ran it earlier, this code, more code can go here never got displayed. So run it. Enter two

integers, let's say 87 and 0.

Now you don't see that ugly error message anymore.

It says Please do not enter a 0. We can say, Please do not enter 0 as the second number as well.

But the important thing is, our program did not terminate abruptly.

So the program execution has continued after the catch block, after the try catch block. Wherever the catch

ends,

after that whatever code we place will be executed.

That is the beauty of exception handling. And that is the reason you see the statement, More code can

go here.

Let's handle it for ArrayIndexOutOfBound as well.

So we can put this code inside try and catch after the for loop.

catch(), remove the try there, Exception,

this throws a ArrayIndexOutOfBoundException. So ArrayIndex, control+space, even it will help you out,

ArrayIndexOutOfBoundException e.

Sysout,

array, we can say beyond array length or index beyond array length.

The more meaningful, the better.

Run it, right click, Run as -> Java application and no more ugly messages.

It prints the values,

then it says index beyond array length. And what is important is the code

after try catch executes. That is the reason we need to handle exceptions in our programs.

So as an assignment please do the same for the StringParser class and then the NullPointerException

in the Test class. Surround them within try catch and display a user friendly message by handling the

appropriate exception. In Test class you'll be handling NullPointerException in here. If you right click

and Run as -> Java application, you need to handle the NumberFormatException.
     
 
what is notes.io
 

Notes.io is a web-based application for 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 12 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

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.