Question: Read through the lab instructions before coming to the lab. Do the pre-lab preparation. Overview and preparation We will work with exceptions and practice debugging

Read through the lab instructions before coming to the lab.

Do the pre-lab preparation.

Overview and preparation

We will work with exceptions and practice debugging For preparation, review the lecture notes on Java exceptions.

Don't forget to submit your work on owl.

Exercise 1: Experimenting with Exceptions

In this exercise, you will experiment with some examples of code that generates exceptions and answer some questions on the examples. Download the Exception Examples 1, 2, 6, 7, 8, 9, 10, 11, and 12 from the Sample Code section of the course webpage. It may be simplest if you put them all in one project, and run them separately as you progress through this Exercise. Note that some of them will (purposely) not compile.

Run examples 1 and 2. They both have an "unchecked exception": one that you do not explicitly need to check for in the code, but will be generated if the conditions for the exception occur.

What is the condition that generates the exception in each of these programs?

Which of the two examples allows the program to continue even if the exception is generated?

Run examples 6 and 7. They both have the same exception, but notice that the try-catch statement is in a different method in each of the examples. This is an example of propagating the exception, as mentioned in the exception lecture notes. (Notice also that these examples show how print statements can be included to allow us to see where we are in the execution of the program, when the variable debug has the value true.)

What is the statement that actually generates the exception in example 6? In example 7?

In example 7, does execution of the method change continue after the statement that generated the exception?

What happens of you change the statement containing debug = true to debug = false ?

Now run example 8. Notice that both the main method and the method change have a try-catch statement. In which method(s) is the exception actually caught in this case?

Run example 12. This is an example that contains multiple possible exceptions. Try running it for an illegal input such as the letter A, and then for a legal integer input such as the value 6. When handling an exception, how many of each of the following clauses can be present in a try/catch statement?

number of try clauses possible?

number of catch clauses possible?

Try to run example 9. Note that it does not compile. This is an example of an exception that is known as a "checked" exception, and the method header must state that it is throwing an exception. Now run examples 10 and 11 using illegal input such as abc. What is the "checked" exception that is generated in examples 9, 10 and 11?

Add a loop to example 11, that asks the user for another input for as long as the input is not valid. You will need to use a while loop and a boolean variable that tells the program whether a valid input has yet been entered.

Exercise 2

Eclipse comes with a debugger which allows you to single-step through your code one statement at a time, trace variables, stop the execution at any time, and find errors in this way. In this exercise you will use the debugger to trace through the program DebuggingExercise.java.

Download the file DebuggingExercise.java and create a new project

Open DebuggingExercise.java and try to run it. You will see that it compiles but does not run.

Check that you have a Debug button in the top right corner of your Eclipse window, beside the Java button. If you don't, select Window - Open Perspective - Debug.

Click on the Debug button to change to the Debug view. You can, at any time, switch back to the Java view by clicking on the Java button in the top right corner.

In the window displaying your code, you will now add a breakpoint to the line for (int j=1; j<=6; j++). You do this by right-clicking in this line at the very left, and then clicking Toggle Breakpoint. You can remove a breakpoint at any time in the exact same way.

When executing your program, the debugger will stop every time this line is reached. Run your program in the debug mode by clicking on Run - Debug (alternatively, F11). The variables will appear in the variable window and the program stops upon reaching the breakpoint for the first time. Now you can use F5 to execute your program one instruction at a time (called single-stepping) or F8 to continue with the execution until the next breakpoint is reached (in a loop the next breakpoint might be the same one again). Note how the variables i and j are changing in the Variables window. While you are doing this, expand some of the rows and columns of the array testArray in the Variables window, to see how the variables are changing.

Fix the error when you find it.

ExceptionExample1.java

public class ExceptionExample1 { public static void main (String[] args) { /* - unchecked exceptions: standard runtime exceptions methods need not state if they throw these - generate an array index out of bounds unchecked exception which will cause a runtime error - another example of an unchecked exception is division by 0 */ final int NUM_STUDENTS = 5; int students[] = new int[NUM_STUDENTS]; students[NUM_STUDENTS] = 1; } }

ExceptionExample2.java

public class ExceptionExample2 { public static void main (String[] args) { /* - unchecked exceptions: standard runtime exceptions methods need not state if they throw these - generate an array index out of bounds unchecked exception which is handled gracefully with a try/catch structure */ final int NUM_STUDENTS = 5; int students[] = new int[NUM_STUDENTS]; try { students[NUM_STUDENTS] = 1; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Sorry, the array element " + NUM_STUDENTS + " does not exist."); } } }

ExceptionExample6.java

public class ExceptionExample6 { private static boolean debug = true; private static final int NUM_STUDENTS = 5; private static final int ILLEGAL_INDEX = 5; /* - try/catch in a method - notice where execution continues after the catch is executed */ public static void change(int[] course) { if (debug) System.out.println("in method change "); try { course[ILLEGAL_INDEX] = 10; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Sorry, index " + ILLEGAL_INDEX + " does not exist."); } if (debug) System.out.println("end of method change"); } public static void main (String[] args) { int students[] = new int[NUM_STUDENTS]; change(students); if (debug) System.out.println("end of main"); } }

ExceptionExample7.java

public class ExceptionExample7 { private static boolean debug = true; private static final int NUM_STUDENTS = 5; private static final int ILLEGAL_INDEX = 5; /* - try/catch at the method invocation level */ public static void change(int[] course) { if (debug) System.out.println("in method change "); course[ILLEGAL_INDEX] = 10; if (debug) System.out.println("end of method change"); } public static void main (String[] args) { int students[] = new int[NUM_STUDENTS]; try { change(students); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Sorry, index " + ILLEGAL_INDEX + " does not exist."); } if (debug) System.out.println("end of main"); } }

ExceptionExample8.java

public class ExceptionExample8 { private static boolean debug = true; private static final int NUM_STUDENTS = 5; private static final int ILLEGAL_INDEX = 5; /* - try/catch in the method and at the method invocation level - notice that the exception is caught once */ public static void change(int[] course) { if (debug) System.out.println("in method change "); try { course[ILLEGAL_INDEX] = 10; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("in method change: index " + ILLEGAL_INDEX + " does not exist."); } if (debug) System.out.println("end of method change"); } public static void main (String[] args) { int students[] = new int[NUM_STUDENTS]; try { change(students); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Sorry, index " + ILLEGAL_INDEX + " does not exist."); } if (debug) System.out.println("end of main"); } }

ExceptionExample9.java

import java.io.*; public class ExceptionExample9 { public static void main (String[] args) { /* - checked exceptions: methods must either handle these exceptions (with a try/catch) or state that they are throwing the exceptions (i.e. not dealing with the exception) with the throws keyword - this will generate a syntax error because the checked exception, IOException, is not handled nor thrown */ BufferedReader keyboard= new BufferedReader (new InputStreamReader(System.in),1); System.out.println("Enter an integer: "); String userTyped = keyboard.readLine(); int value = Integer.parseInt(userTyped); } }

ExceptionExample10.java

 import java.io.*; public class ExceptionExample10 { public static void main(String[] args) throws Exception { /* * - this will compile because the throws keyword in the header says * that all exceptions are thrown, that is, not handled - run this and * type in something that is not an integer (e.g. abc, 3.3) * */ BufferedReader keyboard = new BufferedReader(new InputStreamReader( System.in), 1); System.out.print("Enter an integer: "); String userTyped = keyboard.readLine(); int value = Integer.parseInt(userTyped); } }

  • ExceptionExample11.java
  • import java.io.*; public class ExceptionExample11 { public static void main (String[] args) throws Exception { /* - this handles the NumberFormatException */ BufferedReader keyboard= new BufferedReader (new InputStreamReader(System.in),1); System.out.print("Enter an integer: "); String userTyped = keyboard.readLine(); try { int value = Integer.parseInt(userTyped); } catch (NumberFormatException e) { System.out.println("Hey, " + e.getMessage() + " is not an integer!"); } } }
  • ExceptionExample12.java
  • import java.io.*; public class ExceptionExample12 { public static void main (String[] args) throws Exception { /* - multiple exceptions - notice what happens when there's a possibility for 2 exceptions in the same try */ BufferedReader keyboard= new BufferedReader (new InputStreamReader(System.in),1); System.out.print("Enter an integer: "); String userTyped = keyboard.readLine(); try { int value = Integer.parseInt(userTyped); System.out.println("Divide by zero " + 5/0); } catch (NumberFormatException e) { System.out.println("Hey, that's not an integer!"); } catch (ArithmeticException e) { System.out.println("I don't know how to divide by 0."); } System.out.println("end of main"); } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!