2 Types Exception in Java Program

What is Exception? And 2 types Exception

In simple: Exception is Error.

In java program 2 types exception possible in java program. 1st is compile time exception and 2nd is run time exception.

Compile time is one of the 2 types exception, we already face in programming. Like variable name not match, semicolon missing, class name error, syntax error etc. In simple word, when we compile programs and found error this is called compile time error.

Run time is one of the 2 types exception, in this chapter 10 we will discuss on run time errors with different examples. In simple word, when we run programs and found any exception this is called run time exception.

In this blog program, we see how to run time error stop the program.

 

How to exception work on program – Java Programs

import java.util.*;      

class Exc1

{

            public static void main(String args[])

            {

                        Scanner in1 = new Scanner(System.in);

 

                        System.out.print(“Enter X : “);

                        int x = in1.nextInt();

                        System.out.print(“Enter Y : “);

                        int y = in1.nextInt();

 

int div = x/y; // divided by zero :-> infinity

                        // Run time error stop the program

 

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

                       

// Simple display 5 name in o/p screen

                        String name[] = {“Rakesh”,”Rohan”,”Binny”,”Sonal”,”Nilay”};

                        for(String str : name)

                        {

                                    System.out.println(str);

                        }

            }

}

Output:

Output -1

Enter X: 23

Enter Y: 12

Div = 1

Rakesh

Rohan

Binny

Sonal

Nilay

 

Output -2

Enter X: 56

Enter Y: 0

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

            At Exc1.main(Exc1.java:19)

 

So this is basic example run time exception. Here I mention 2 output of this program.

1st output is perfect program, so when other user put perfect number of x and y variable then got perfect answer of this program.

2nd output is error program, so when other user put 0 value into y variable, program generate ArithmeticException error.

 

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

426total visits,2visits 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