InClassExercise21

InClassExercise21 - 1 Copy the InClass21 folder from the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CS112: In-class Exercise 21 try & catch Exception 0 . Review of try-catch syntax. For any code that might throw an exception. .. try{ any code here that might throw an exception }catch(Exception exceptionVariable){ //the catch clause this part is executed only if an exception was thrown above } Note that the exception variable can be any legal variable name. In the catch clause, you may use the exception variable to gain more information such as… System.out.println("Exception information: " + exceptionVariable); It is possible to distinguish different types of exceptions. .. try{ any code here that might throw an exception }catch(NumberFormatException exc){ this part is executed only if a NumberFormatEexception was thrown } catch(FileNotFoundException exc){ this part is executed only if a FileNotFoundEexception was thrown } Read java documentation to see what classes and what methods throws exceptions when you encounter classes that throw exceptions.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1. Copy the InClass21 folder from the usual locations. 2. Open the project using BlueJ. 3. Edit the source file for InClass21 . 4. Exercise A. Write a void method addUpAFile() that will ask the user for a file name. Use a try-catch statement to attempt to open the file, and if successful, computes the number of integers in the file and their sum. You may use the existing code for previousExample() as help. 5. HW#10. Write a void method addUpTwoNumbers() that will ask the user to input two integers. Use a try-catch statement to make sure that they are indeed two integers. Read the input in as two strings and use Integer.parseInt(String) to parse the two strings into integers. If they are not integers, output some message to that effect. You may use the existing code for previousExample2() as help. Bonus: Write this method so that it keeps asking the user for input until the user types in legitimate integers....
View Full Document

This lab report was uploaded on 04/08/2008 for the course CSCI 112 taught by Professor Yuen during the Fall '07 term at Lake Forest.

Ask a homework question - tutors are online