Question

Exception handling is a powerful tool used to help programmers understand exception errors. This tool separates...

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.io.InputStreamReader;
import java.util.ArrayList;

public class Ex02 {

    public static void main(String[] args) throws IOException {

        BufferedReader userInput = new BufferedReader
            (new InputStreamReader(System.in));

        ArrayList<String> myArr = new ArrayList<String>();
        myArr.add("Zero");
        myArr.add("One");
        myArr.add("Two");
        myArr.add("Three");        
        System.out.println(myArr.get(7));
               
    }
}

Starting with this provided code, add the following functionality:

  1. Use a Try/Catch block so that the exception is caught and the program exits a bit more gracefully. Save this file as TryCatch.java. (Be sure to rename the Public Class accordingly.)
  2. Starting with the provided code again (without the Try/Catch block), fix the code so that it runs correctly. That is to say, it should display the last item in the ArrayList. Save this file as Fixed.java. (Be sure to rename the Public Class accordingly.)

Compile, run, and check the results.

Submit the following:

  1. The source file for each of the classes created (If more than one file has been created, zip them into one .zip file for posting.)
  2. An MS .doc file with explanations/comments of your solution and the results of a test run, including a screen shot picture
0 0
Add a comment Improve this question Transcribed image text
Answer #1

TryCatch.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class TryCatch {

   public static void main(String[] args) throws IOException {

       try {
           BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

           ArrayList<String> myArr = new ArrayList<String>();
           myArr.add("Zero");
           myArr.add("One");
           myArr.add("Two");
           myArr.add("Three");
           System.out.println(myArr.get(7));
       } catch (Exception e) {
           System.out.println("Exception caught:");
       }

   }
}

============================================================
SEE OUTPUT





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

Fixed.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Fixed {

public static void main(String[] args) throws IOException {

BufferedReader userInput = new BufferedReader
(new InputStreamReader(System.in));

ArrayList<String> myArr = new ArrayList<String>();
myArr.add("Zero");
myArr.add("One");
myArr.add("Two");
myArr.add("Three");
System.out.println(myArr.get(3));

}
}

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

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Exception handling is a powerful tool used to help programmers understand exception errors. This tool separates...
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
  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR...

    /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR NAME HERE * Class: PRG/420 * Creation Date: TYPE TODAY'S DATE HERE ************************************************************************************* * Program Summary: * This program converts a given date to a string. * The code includes exception handling for a ParseException. ************************************************************************************/ package prg420week5_codingassignment; import java.util.*; // wildcard to import all the util. classes import java.text.*; // wildcard to import all the text classes public class PRG420Week5_CodingAssignment { public static...

  • 3. Handling exceptions with a finally clause a. Download the following file from the class website:...

    3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code): i. Embedded in the code is a note showing the location for the try statement. ii. The first line of the catch block should look like this: catch(IOException e) {...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • Please complete the give code (where it says "insert code here") in Java, efficiently. Read the...

    Please complete the give code (where it says "insert code here") in Java, efficiently. Read the entire input one line at a time and then output a subsequence of the lines that appear in the same order they do in the file and that are also in non-decreasing or non-increasing sorted order. If the file contains n lines, then the length of this subsequence should be at least sqrt(n). For example, if the input contains 9 lines with the numbers...

  • C++ Your assignment this week is to make your fractionType class safe by using exception handling....

    C++ Your assignment this week is to make your fractionType class safe by using exception handling. Use exceptions so that your program handles the exceptions division by zero and invalid input. · An example of invalid input would be entering a fraction such as 7 / 0 or 6 / a · An example of division by zero would be: 1 / 2 / 0 / 3 Use a custom Exception class called fractionException. The class must inherit from exception...

  • Hello can somebody please help me with Project 15-3 File Cleaner assignment? This project is to...

    Hello can somebody please help me with Project 15-3 File Cleaner assignment? This project is to be done while using Java programming. Here are what the assignment says… Create an application that reads a file that contains an email list, reformats the data, and writes the cleaned list to another file. Below are the grading criteria… Fix formatting. The application should fix the formatting problems. Write the File. Your application should write a file named prospects_clean.csv. Use title case. All...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

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