Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8473915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:29:08+00:00 2026-06-10T17:29:08+00:00

This is a code sample. If I was to enter numerator: 5 and Denominator:

  • 0

This is a code sample. If I was to enter numerator: 5 and Denominator: 0

I get an exception like this:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionHandling.DivideByZeroExceptions.quotient(DivideByZeroExceptions.java:10)
at ExceptionHandling.DivideByZeroExceptions.main(DivideByZeroExceptions.java:22)

I know I have to include ( throws Arithmetic Exception )
But, How would I know that I need to use a inputMismatchException?

 // Try DivideByZeroExceptions

  public class DivideByZeroExceptions {

public static int quotient(int numerator, int denominator) {
    return numerator / denominator;
}

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter an integer numerator: ");
    int numerator = input.nextInt();
    System.out.println("Please enter an integer denominator: ");
    int denominator = input.nextInt();

    int result = quotient(numerator, denominator);
    System.out.printf("\nResult: %d / %d = %d\n", numerator, denominator,
            result);

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:29:09+00:00Added an answer on June 10, 2026 at 5:29 pm

    Not sure exactly what your asking about inputMismatchException, but this is what you should do:

    public static int quotient(int numerator, int denominator) {
        if(denominator == 0)
            throw new IllegalArgumentException("Cannot divide by 0!");
        return numerator / denominator;
    }
    

    IllegalArgumentException extends RuntimeException, not just Exception. As such, it will simply stop execution of the thread after it occurs, so it doesn’t need to be caught/thrown (of course you can still catch it outside the method in order to prevent the Thread from stopping).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Here is some hypothetical code sample: if (e.KeyCode == Keys.Enter) { if (this.CurrentElement ==
I am studying this code sample: class Program { static void Main(string[] args) {
First consider this sample C++ code: std::string input1, input2, input3; std::cout << Enter Input
I'm reading this code sample : And since I don't know C#, I decided
For one or another reason this code sample gives 48 hours instead of 24,
In this particular code sample I want to reference the second overloaded method (int
I am new to scheme. This is code sample from SICP course of MIT.
I'm trying to learn the Enterprise Library. I found this useful code sample to
In this sample code the URL of the app seems to be determined by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.