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.
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. "); } }
Select a Java program that contains an exception error. The exception error can be one that...
Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage( ) method is inherited from this class? What are the two broad catagories of Exceptions in Java? If an Exception is not a checked exception it must extend what class? What is the difference between a checked Exception and an unchecked Exception? What are the two options a programmer has when writing code that may cause a checked Exception to be thrown? Can a...
please write JAVA code: Your Own Exception Class Write a text-based (non-GUI) program to read in file that contains only positive numbers and outputs to the screen the sum of all numbers. Create your own exception class that describes the situation of a non-positive number. In your program, use this exception to handle this situation. Use other exception classes from the Java standard library to handle other I/O situations.
in java Write a program that contains a bounds error. Run the program. What happens on your computer?
Do the following using JAVA language. a) Create an Error. (You can create any error.) b) Handle the error. c) Create a custom exception which will give a kind message. d) Handle both of the errors by using only one try-catch block.
Source material: Object-Oriented Data Structures Using Java, 3rd Edition by Nell Dale. What is the answer to exercise 13 in chapter 3 (page 232)?? "A. Create a "standard" exception class called ThirteenException. B. Write a program that repeatedly prompts the user to enter a string. After each string is entered the program outputs the length of the string, unless the length of the string is 13, in which case the ThirteenException is thrown with the message "Use thirteen letter words...
in Java What's wrong with your program if it produces this error message when you try to run it (be specific) java.lang.ArrayIndexOutofBoundsException
Exception handling is a powerful tool used to help programmers understand exception errors. This tool separates the error handling routine from the rest of the code. In this application, you practice handling exception errors. You use sample code that was purposefully designed to generate an exception error, and then you modify the code so that it handles the errors more gracefully. For this Assignment, submit the following program: The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import...
JAVA Problem Description: For this assignment, you will be writing your own exception class and you will handle run-time exceptions resulting from one particular input situation. Specifics: 1) Design and implement a program that has an exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. 2) In the main driver of the program (call this class MyExceptionTest), read strings from the user until the user enters “DONE”. If a...
JAVA: Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console.
A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...