Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101: Programming Methodology Aaron Tan.

Similar presentations


Presentation on theme: "CS1101: Programming Methodology Aaron Tan."— Presentation transcript:

1 CS1101: Programming Methodology http://www.comp.nus.edu.sg/~cs1101x/ http://www.comp.nus.edu.sg/~cs1101x/ Aaron Tan

2 2 This is Week 12  Week 10:  Chapter 11: Type Details and Alternate Coding Mechanisms  This week:  Chapter 14: Exception Handling  Chatper 15: Files

3 3 Writing robust programs (1/3) Suppose you have this statement int value = stdIn.nextInt(); So far, we are assuming that the user always follows instructions and never enters the wrong data. But what if the user enters the following in response to the above statement?  A string (eg: “apple”)?  A real number (eg: 12.3)?

4 4 Writing robust programs (2/3) Refer to WrongInput.java  User enters a string:  User enters real number: Enter an integer: apple Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:819) at java.util.Scanner.next(Scanner.java:1431) at java.util.Scanner.nextInt(Scanner.java:2040) at java.util.Scanner.nextInt(Scanner.java:2000) at WrongInput.main(WrongInput.java:9) Enter an integer: 12.34 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:819) at java.util.Scanner.next(Scanner.java:1431) at java.util.Scanner.nextInt(Scanner.java:2040) at java.util.Scanner.nextInt(Scanner.java:2000) at WrongInput.main(WrongInput.java:9) An exception is thrown.

5 5 Writing robust programs (3/3) An exception is thrown when an error occurs. If the exception is not caught, the program crashes. We would like to catch the exception so that the program does not crash. Let’s go on to Chapter 14 to see how to handle exceptions.

6 6 Using throws to postpone catch Refer to StudentList2.java and StudentList2Driver.java For checked exceptions, if you do not have a try-catch block, then you must add ‘throws’ at the heading of the method, or it will not compile.

7 7 The finally Block Sometimes there are situations where we need to take certain actions regardless of whether an exception is thrown or not. For example, we need some “cleanup” code, like closing a file. Use a finally block. Refer to WriteToFile.java

8 8 Summary: try-catch block Remaining statements in the try block is skipped. try {... } catch (Exception e) {... } Assume throws an exception. Assume throws an exception. Exception Statements in the catch block are executed. And the execution continues to the next statement try {... } catch (Exception e) {... } No Exception Statements in the catch block are skipped. All statements in the try block are executed. Ack: Thomas Wu

9 9 Summary: multiple catch blocks All catch blocks are skipped. try {... }... } No Exception All statements in the try block are executed and throw no exceptions. Remaining statements in the try block is skipped. try {... }... } Assume throws an exception and is the matching block. Assume throws an exception and is the matching block. Exception Statements in the matching catch block are executed. Ack: Thomas Wu

10 10 Summary: with finally block No Exception try {...... }...... } finally {... } Assume throws an exception and is the matching block. Assume throws an exception and is the matching block. Exception try {...... }...... } finally {... } finally block is executed. Ack: Thomas Wu

11 11 Summary: Hierarchy of exceptions There are over 60 classes in the hierarchy. Here are just some. Ack: Thomas Wu

12 12 Handling files in UNIX  We have seen file processing in Java.  Let me introduce some features in UNIX that handle files (we did it in our intro lab, if you still remember!)  This is not part of Java.

13 13 UNIX File Input Redirection <:To redirect input from a file $ java MyProgram < inputFile The text file inputFile contains the input data. $ java MySum Enter 3 integers: 4 2 3 Sum = 9 $ java MySum < sum.in Enter 3 integers: Sum = 9 $ cat sum.in 4 2 3

14 14 Example MySum.java import java.util.*; class MySum { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.print("Enter 3 integers: "); int a = stdIn.nextInt(); int b = stdIn.nextInt(); int c = stdIn.nextInt(); int sum = a + b + c; System.out.println("Sum = " + sum); }

15 15 UNIX File Output Redirection >:To redirect output to a file (>>: to append to a file) $ java MyProgram > outputFile The text file outputFile contains the output data. $ java MySum Enter 3 integers: 4 2 3 Sum = 9 $ java MySum > sum.out 4 2 3 $ cat sum.out Enter 3 integers: Sum = 9

16 16 Announcement/Reminder  Lab #5  Deadline: 5 November (Wednesday), 2359 hr.  PE  Please refer to website http://www.comp.nus.edu.sg/~cs1101x/3_ca/pe.html http://www.comp.nus.edu.sg/~cs1101x/3_ca/pe.html  Consultation @ my office (COM1-03-12)  Wednesday (5 Nov) 3 – 6 pm

17 17 This is Week 12  Next week (last week!)  Chapter 12: Aggregation, Composition, and Inheritance

18 18 End of file


Download ppt "CS1101: Programming Methodology Aaron Tan."

Similar presentations


Ads by Google