Write a program in Java to develop user defined exception for 'Divide by Zero' error.

 

AIM: Write a program in Java to develop user-defined exception for 'Divide by Zero' error.

CODE:

import java.util.Scanner;

class DivideByZeroException extends Exception

{

            DivideByZeroException()

            {

                        super();

            }

            DivideByZeroException(String s)

            {

                        super(s);

            }

}

class dbz

{

            public static void main(String args[])

            {

                        char con;

                        do{

                                    Scanner s = new Scanner(System.in);

                                    System.out.print("\nEnter Dividend: ");

                                    int a = s.nextInt();

                                    System.out.print("\nEnter Divisor: ");

                                    int b = s.nextInt();

                                    try

                                    {

                                                div(a,b);

                                    }

                                    catch(DivideByZeroException e)

                                    {

                                                System.out.println(e);

                                                System.out.println("\n-------------");

                                                e.printStackTrace();

                                                System.out.println("\n-------------");

                                                System.out.println(e.getMessage());

                                    }

                        System.out.print("\nDo you want to continue enter y or Y for yes: ");

                        con = s.next().toUpperCase().charAt(0);

                        }while(con=='Y');

            }

            static void div(int a,int b) throws DivideByZeroException

            {

                        if(b<=0)

                        {

                                    throw new DivideByZeroException("B should not be zero");

                        }

                        float div = a/b;

                        System.out.println("\nDivision of "+a+" and "+b+" is "+div);

            }

           

}

 

OUTPUT:



Comments

Popular posts from this blog

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a Java Program with class Rectangle having instance variable width, length, area and colour. Create methods like setData() and area().Create two object of Rectangle and compare their area and colour. If area and colour both are the same for the objects then display “Matching Rectangles”, otherwise display “Non matching Rectangle”.