SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Exception Handling
Exercise:
Programming Projects
Question:2 | ISBN:9780132830317 | Edition: 5

Question

Here is a snippet of code that inputs two integers and divides them:

Scanner scan = new Scanner(System.in);

int n1, n2;

double r;

n1 = scan.nextInt();

n2 = scan.nextInt();

r = ( double) n1 / n2;

Place this code into a try-catch block with multiple catches so that different error messages are printed if we attempt to divide by zero or if the user enters textual data instead of integers ( java.util.InputMismatchException ). If either of these conditions occurs, then the program should loop back and let the user enter new data.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// DivisionByZeroException.java
public class DivisionByZeroException extends Exception
{
	public DivisionByZeroException()
	{
		super("Division by zero!");
	}
	
	public DivisionByZeroException(String msg)
	{
		super(msg);
	}
}
// ExceptionDemo.java
import java.util.Scanner;
import java.util.InputMismatchException;
public class ExceptionDemo 
{
	public static void main(String[] args) 
	{
		Scanner scan = new Scanner(System.in);
		int n1, n2;
		double r;
		boolean solved = false;
		
		while(!solved)
		{
			try
			{
				System.out.print("Please enter a value for n1: ");
				n1 = scan.nextInt();
				
				System.out.print("Please enter a value for n2: ");
				n2 = scan.nextInt();
				
				if(n2 == 0)
					throw new DivisionByZeroException();
				
				r = (double) n1 / n2;
				
				System.out.println("Result:  " 
								+ n1 + "/" + n2 + " = " + r);
				
				solved = true;
			}		
			catch(InputMismatchException imme)
			{
				scan.nextLine();
				System.out.println("The user enters textual data 
							instead of integers. Try again! \n");
			}
			catch(DivisionByZeroException dbze)
			{
				scan.nextLine();
				System.out.println("The user attempts to 
								divide by zero. Try again! \n");
			}
			catch(Exception e)
			{
				scan.nextLine();
				System.out.println(e.getMessage());
			}
		}
	}
}

Output:

Please enter a value for n1: 6
Please enter a value for n2: R
The user enters textual data instead of integers. Try again! 

Please enter a value for n1: 28
Please enter a value for n2: 0
The user attempts to divide by zero. Try again! 

Please enter a value for n1: 81
Please enter a value for n2: 3
Result:  81/3 = 27.0

 

0 0

Discussions

Post the discussion to improve the above solution.