Objective: Practice Throwing Exceptions using the throws clause and throw statem
ID: 3729289 • Letter: O
Question
Objective: Practice Throwing Exceptions using the throws clause and throw statement Write code that explicitly throws one of the standard Java exceptions. You must include a throws clause and a throw statement The method divide accepts three integers , y and z and returns the yalue of 425/(xty*z). Modify this method to also throw an Exception object with the message "dividing by zero", if evaluating the equation will result in a divide by zero error NOTE: Do NOT catch the Exception in the method simply throw an Exception when one is needed. Your method should still return an accurate value when there is no divide by zero problem. SUBMITRESET 1 of 1: Tue Mar 13 2018 17:34:21 GMT-0400 (EDT) l class Evaluate statie int dividelint x,int y int reealt . 425/eun; return reaulti 121Explanation / Answer
import java.util.*;
import java.io.*;
public class Test {
public static int divide(int x,int y,int z) throws java.lang.Exception
{
int result;
int sum = (x+y+z);
if(sum==0)
throw new Exception("Dividing by Zero");
else
result = 425/sum;
return result;
}
public static void main (String[] args) throws java.lang.Exception
{
int x,y,z;
Scanner sc =new Scanner(System.in);
System.out.println("Enter number x :");
x = sc.nextInt();
System.out.println("Enter number y :");
y = sc.nextInt();
System.out.println("Enter number z :");
z = sc.nextInt();
System.out.println("Result :"+ divide(x,y,z));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.