0% found this document useful (0 votes)
151 views

Lab Exception Handling PDF

This document discusses a Java programming lab on exception handling. The lab has students: 1) Run a program that generates random numbers and observes whether it results in division by zero exceptions. 2) Create a method that throws an ArithmeticException and call it from main, with and without try/catch blocks to observe the output. 3) Modify the exception thrown to IllegalAccessException, requiring changes to catch the correct exception type.

Uploaded by

theomega76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Lab Exception Handling PDF

This document discusses a Java programming lab on exception handling. The lab has students: 1) Run a program that generates random numbers and observes whether it results in division by zero exceptions. 2) Create a method that throws an ArithmeticException and call it from main, with and without try/catch blocks to observe the output. 3) Modify the exception thrown to IllegalAccessException, requiring changes to catch the correct exception type.

Uploaded by

theomega76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSCD211 Programming Principles II 10/26/2011

CSCD211 Lab Exception Handling

Using Java exception handling.


Key in the following Java program that will use exception handling to observe random number
generation:

Eastern Washington University


CSCD211 Programming Principles II 10/26/2011

Run the program three times, noting the number of good and bad results:

1a. Number of occurrences:

1. Good_______________________ Divide by zero_______________________

2. Good_______________________ Divide by zero_______________________

3. Good_______________________ Divide by zero_______________________

1b. What is the deciding factor for division by zero? __________________________


______________________________________________________________________

2. Create a new function called throwOne() that will throw an arithmeticException:

Comment out the existing content of main() and add a call to throwOne() plus a call to s.o.p to
indicate a normal end-of-job:

public static void main(String[] args)


{
throwOne();
System.out.println("Exiting normally...");
}

2a. Run your program. What is the output? ________________________________________


_____________________________________________________________________________
_____________________________________________________________________________

Eastern Washington University


CSCD211 Programming Principles II 10/26/2011

Add a try-catch block to surround the call to throwOne():

2b. Run your program again. Now what is the output? _______________________________
_____________________________________________________________________________
_____________________________________________________________________________

3. Change the exception that is thrown in throwOne() to IllegalAccessException:

3a. Try to compile your program. Why do you get an error? ________________________
__________________________________________________________________________

3b. How would you fix the programs problem? __________________________________


__________________________________________________________________________

3c. What is your programs output after youve fixed the problem? ___________________
___________________________________________________________________________

Eastern Washington University

You might also like