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
  • (I don't need code for this question I only need explanation how to solve the question.)...

    (I don't need code for this question I only need explanation how to solve the question.) 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,...

  • please help. About java tcp. this is what i have so far. how to convert input...

    please help. About java tcp. this is what i have so far. how to convert input value into ASCII sequence For example the message: “Hello World” would become “Ifmmp!Xpsme”For example the message: “Hello World” would become “Ifmmp!Xpsme”in server.java. Client.java: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.net.SocketTimeoutException; public class Client { public static void main(String[] args) throws IOException {    Socket client = new Socket("127.0.0.1", 20006); client.setSoTimeout(10000);    BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); PrintStream out...

  • 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...

  • Can someone please help me? I'm having trouble with this assignment and have been stuck trying...

    Can someone please help me? I'm having trouble with this assignment and have been stuck trying to figure it out for over an hour. Here's the assignment details below: For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text flie, sort and store the contents of the text file into an ArrayList, then write the sorted contents...

  • Having trouble with my code, it keeps giving me an error every time I run it....

    Having trouble with my code, it keeps giving me an error every time I run it. Can someone please help me understand what I'm doing wrong? *********CODE*************** // Java program to read the file contents, sort it and output the sorted content to another file import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; public class Datasort { public static void main(String[] args) throws IOException { File fin = new File("input.txt");...

  • 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 =...

  • 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) {...

  • /************************************************************************************ * 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...

  • 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...

  • 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...

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