Question

Select a Java program that contains an exception error. The exception error can be one that...

Select a Java program that contains an exception error. The exception error can be one that you have encountered yourself or one you located using the Internet. Describe your chosen exception error and explain potential implications. Be sure to include the actual code in your posting. Prior to submission, check your colleagues' postings, as your post must contain a different error message from your colleagues.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change.

Thank You !!


In this program, we will look into ArithmeticException, this exception is thrown when we try to divide a number by zero - which is an invalid operation.

If we allow this operation, exception is thrown and the program crashes. To prevent this from happening we must always check the denominator is not zero whenever we try to divide a number by zero.


========================================================================================


import java.util.Scanner;

public class ExceptionEncountered {


    public static void main(String[] args) {


        // Arithmetic Exception
        // when we try to divide a number by zero

        Scanner scanner = new Scanner(System.in);

        // we ask user to enter the numerator
        System.out.print("Enter numerator: ");
        int num = scanner.nextInt(); // store it in num variable
        // ask user to enter denominator
        System.out.print("Enter denominator: ");
        int den = scanner.nextInt(); // store it in den variable

        // display the result of dividing num by den
        System.out.println("Num divided by Den equals to : " + (num / den));

        // if user enters denominator as 0 then the program crashes
        // the program throws an run time exception ArithmeticException
        // stating we are trying to divide a number by zero
        
        // so to prevent this we first need to ensure that denominator is not equal
        // to zero using an if condition
        


    }
}

======================================================================================

// Program using if condition to prevent program from crashing because of exception

import java.util.Scanner;

public class ExceptionEncountered {


    public static void main(String[] args) {


        // Arithmetic Exception
        // when we try to divide a number by zero

        Scanner scanner = new Scanner(System.in);

        // we ask user to enter the numerator
        System.out.print("Enter numerator: ");
        int num = scanner.nextInt(); // store it in num variable
        // ask user to enter denominator
        System.out.print("Enter denominator: ");
        int den = scanner.nextInt(); // store it in den variable

        // check first den is not equal to 0 and then does the division
        if (den != 0)
            System.out.println("Num divided by Den equals to : " + (num / den));
        else // if the den is zero, print a error message, this will help program not to crash
            System.out.println("Invalid division operation. ");


    }
}

Add a comment
Know the answer?
Add Answer to:
Select a Java program that contains an exception error. The exception error can be one that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT