Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Exception Handling Exercise: Division Operation - Prof. Guerra, Assignments of Java Programming

This java program demonstrates exception handling during division operation. The user is prompted to input two numbers, which are then converted to integers and divided. The program catches arithmeticexception and numberformatexception, displaying error messages and prompting the user to try again. The finally block is used to print motivational messages.

Typology: Assignments

2020/2021

Uploaded on 04/01/2022

zurbito-jacque-landrito
zurbito-jacque-landrito 🇵🇭

5 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
// import the package to accept the input
import java.io.*;
public class ExceptionExercise1
{
public static void main(String[] args) throws Exception
{
// create object for the input
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
// create and declare the local variables
String first;
String second;
int dividend;
int divisor;
int answer;
try
{
//Ask the user for the first number
System.out.print("Enter the first number: ");
first = dataIn.readLine();
//Ask the user for the second number
System.out.print("Enter the second number: ");
second = dataIn.readLine();
//convert the input to numbers
dividend = Integer.parseInt(first);
divisor = Integer.parseInt(second);
//perform division
answer = (dividend/divisor);
//display the result
System.out.println("The answer is " + answer + ".");
}
catch(ArithmeticException ex)
{
System.out.println("Error! " + ex.getMessage() + ".");
System.out.println("Try Again.");
}
catch(NumberFormatException ex)
{
System.out.println("Error! " + ex.getMessage() + ".");
System.out.println("Try Again.");
}
finally
{
System.out.println("Programming is fun.");
System.out.println("I am starting to love it.");
}
}
}
pf2

Partial preview of the text

Download Java Exception Handling Exercise: Division Operation - Prof. Guerra and more Assignments Java Programming in PDF only on Docsity!

// import the package to accept the input import java.io.*; public class ExceptionExercise { public static void main(String[] args) throws Exception { // create object for the input BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); // create and declare the local variables String first; String second; int dividend; int divisor; int answer; try { //Ask the user for the first number System.out.print("Enter the first number: "); first = dataIn.readLine(); //Ask the user for the second number System.out.print("Enter the second number: "); second = dataIn.readLine(); //convert the input to numbers dividend = Integer.parseInt(first); divisor = Integer.parseInt(second); //perform division answer = (dividend/divisor); //display the result System.out.println("The answer is " + answer + "."); } catch(ArithmeticException ex) { System.out.println("Error! " + ex.getMessage() + "."); System.out.println("Try Again."); } catch(NumberFormatException ex) { System.out.println("Error! " + ex.getMessage() + "."); System.out.println("Try Again."); } finally { System.out.println("Programming is fun."); System.out.println("I am starting to love it."); } } }