NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

In this and the n ext bonus lectures, we will quickly try out and summarize the various important exceptions

we see as developers on a day to day basis.

To do that, go to the source package in Exception handling project right click create a new class.

Let's start with the array index out of bound exception array index out of bound demo.

Put it in a package called Most Wanted.

These are the popular exceptions that we see on a day to day basis, which we need to handle with a

main method.

Finish double click to maximize control D to delete that.

Start by defining a array of type integer is equal to new int of say 20.

Use a Sysout.

Array Index of zero.

No issues.

Copy.

Paste it.

Try

Array index of 20 format it

Right click.

Run the program once you save it.

And you will immediately see the first one works.

The entire array is initialized to zero because we haven't assigned any values to you see zero when

we access a of zero.

But when you try to access a of 20, you see a array index out of bound, 20 is out of bound because

array index starts with zero, goes all the way till the array length minus one.

So here if I try a dot length minus one we'll see no exceptions, zero and zero.

So make sure in real time applications, whenever you are dealing with arrays, we have to be sure that

we are not going out of bounds.

We have to do those checks before we do the operations next.

Popular one and a nightmare to the support people is the null pointer exception.

Create a new class, Call it null pointer demo with a main method finish double click to maximize control

D String s is equal to.

Hello.

Do a sysout s dot use any methods length.

Run this program.

We have no issues.

Everything works as expected now string s is equal to null.

Run the program and you see a null pointer exception right on that line there because s is null in real

time

Programs will not be receiving or will not be using this string like this.

And right away use it here.

We'll be receiving this string from other methods and other classes.

That is when we have to be careful and do a check before we use those objects.

Not only strings, any object that can be null, we'll have to perform a null check.

And once we do the null check, we do a Sysout. here

It has a warning because it knows that we have initialized the string to null.

But this is not how we'll be using it.

We'll receive the string from somewhere.

And before we use that object or string, we do a null check and then we'll never receive a null pointer

exception if it is null.

Next is a Stack overflow exception.

We usually see this in recursive programs if we don't have a proper exit condition.

Create a class stack overflow when the stack runs out of its space.

Finish.

Create a couple of methods public static void so that it will be easy to invoke from the main method

without creating an instance of the class.

Sysout.

Method one, We don't even need this because this will never be printed.

Or even if it is printed, we don't care.

Next is Method two.

We have two methods within method one I invoke method two and within method 2 I invoke method 1 a

circular invocation there Ctrl D here I invoke one of the methods.

It could be method 1 or 2 in the main method because they are static, I can directly invoke them right

click, here

I should say method two in the print statement.

Right click Run the program As soon as we run this, we will see that it will eventually run out.

The stack is overflowed because for the functions, for the methods, there is a stack that gets created.

And as these methods keep calling each other in a continuous basis without stopping, we see a stack

overflow.

Especially this happens when you are working with recursive programs.

You need to have a proper exit conditions.
     
 
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.