Question

JAVA CODE This program causes an error and crashes. Compile and give it a test run,...

JAVA CODE

This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown.
Then do the following to fix the problem

1. Write code to handle this exception
2. Use try, catch and finally blocks.
3. Display, the name of the exception
4. Display a message from the user about what happened.

Here is the starting file

BadArray.java:

public class BadArray {

        public static void main(String[] args) {
                
                 // Create an array with 3 elements.
              int[] numbers = { 1, 2, 3 };

              // Attempt to read beyond the bounds
              // of the array.
              for (int i = 0; i <= 3; i++)
                 System.out.println(numbers[i]);
                 
        }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class BadArray {
    public static void main(String[] args) {
        // Create an array with 3 elements.
        int[] numbers = { 1, 2, 3 };

        try {
            // Attempt to read beyond the bounds
            // of the array.
            for (int i = 0; i <= 3; i++)
                System.out.println(numbers[i]);
        }catch (ArrayIndexOutOfBoundsException aob){
            System.out.println("ArrayIndexOutOfBoundsException: "+aob.getMessage());
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
JAVA CODE This program causes an error and crashes. Compile and give it a test run,...
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
  • write a program to handle an exception that is generated when a program attempts to write...

    write a program to handle an exception that is generated when a program attempts to write beyond the end of an array in a lower scope such that that the exception is handled at a higher scope. java please write simple code so I can understand. use the code below public class Main { public static void main(String [] args) { int [] values = {1,2,3,4}; try { int theSum= calculate_sum(values); } catch (expection arrayindexoutofBoundsexpection) { //System.out.println(“tired to access array...

  • in java please fix code between 14 and 20 to make the code compile Write a...

    in java please fix code between 14 and 20 to make the code compile Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10). The array's size may differ from 4. Display ari ay Values 1 pas [ pa: 6 public void displayValues(int [] arrayVals) { 7 int i; 8 9 for (i = 0; i < arrayVals.length; ++i) { 19 System.out.print(arrayVals[i] +...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • What are the results of the program as written? What results would the program produce if...

    What are the results of the program as written? What results would the program produce if you changed the value of a from 0 to 3? ************************************************************************************* * Program Summary:    *   This code includes simple try/catch (exception handling) functionality. * We put some arithmetic code into a "try" block so that we can "catch" an exception * that is thrown during the course working through the arithmetic. * We need to know specifically what kind of exception we intend...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • Java questions QUESTION 8 Assume the following classes are each defined in their own java files...

    Java questions QUESTION 8 Assume the following classes are each defined in their own java files and compile public class A public void display { System.out.println("A's display"); public class B extends A{ public void display { System.out.println("B's display"); public class C extends A public void display { System.out.println("C's display"); public class D { public static void main(String args) { 3: int i = ((int)(Moth.random(* 10)) if (i == 0) a = new BO: else if (i == 1) a =...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program:...

    Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program: class MultithreadingDemo extends Thread public void runot try{ System.out.println (--Multithread.countDown): catch (Exception e) w public class Multithread{ public static int countDown = 10; public static void main(String[] args) { for (int i = 0; i < 3; i++) { MultithreadingDemo object = new MultithreadingDemol); object.start(); } میه

  • Please help me answer these questions in java programming language. Please answer it line after line...

    Please help me answer these questions in java programming language. Please answer it line after line so that I can understand please. Does it compile or not? No programming is done in this exercise, simply answer the questions asked in the code given below. class Exemple { /*Explain why this code does not compile */ public void m1() { foo(); } public int foo() throws Exception { throw new Exception(); } /*Explain why this code is not considered as good...

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

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