Finally : 1 step to handle all errors

In all advance level exception handling, all programmer use finally keyword to prevent run time error.

Let’s take one example:

User-1 transfers amount to another User-2 account. Amount withdraws from User-1 and suddenly some hardware or networking error generated. That time finally block removes all transfer amounts.

 

Example on Finally in exception handling

class Exc11

{

            public static void main(String args[])

            {

                        try

                        {

                                    int x = 34;

                                    int y = 0;

                                    int div = x/y;

                                    System.out.println(“Div = ” + div);

                        }

                        /*catch(Exception ex)

                        {

                                    System.out.println(ex);        

                        }*/

                        finally

                        {

                                    // Close Your Database Connection etc..

                                    for(int i=1;i<=5;i++)

                                    {

                                                System.out.println(“I = ” + i);

                                    }

                        }

            }

}          

Output:

Output 1: [If y is not zero]

Div = 17

I = 1

I = 2

I = 3

I = 4

I = 5

 

Output 2: [If y is zero and use with catch]

Java.lang.ArithmeticException: / by zero

I = 1

I = 2

I = 3

I = 4

I = 5

 

Output 3: [If y is zero and use without catch]

I = 1

I = 2

I = 3

I = 4

I = 5

Exception in thread “main” java.lang.ArithmeticException: / by zero

At Exc11.main (Exc11.java:17)

 

Our static program provides Output 3. For Output1 and output2, we have to see video.

Video available on YouTube now.

 


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


Earn money online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

899total visits,3visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

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

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint