Docstoc

Summary

Document Sample
Summary Powered By Docstoc
					CS1101: Programming Methodology
http://www.comp.nus.edu.sg/~cs1101x/


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




                                             2
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)?



                                                  3
Writing robust programs (2/3)
   Refer to WrongInput.java
                                                       An exception is thrown.
     User   enters a string:
       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)



     User   enters real number:
       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)




                                                                                 4
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.




                                                   5
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.




                                                     6
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




                                                 7
  Summary: try-catch block
              Exception                                  No Exception
  try {                                      try {
      <t-stmt-1>                                 <t-stmt-1>
                   Assume <t-stmt-3>
      <t-stmt-2>   throws an exception.          <t-stmt-2>    All statements in
                                                               the try block are
      <t-stmt-3>                                 <t-stmt-3>
                                                               executed.
      <t-stmt-4>     Remaining                   <t-stmt-4>
      . . .          statements in the           . . .
                     try block is skipped.
      <t-stmt-n>                                 <t-stmt-n>

  } catch (Exception e) {                    } catch (Exception e) {
      <c-stmt-1>                                 <c-stmt-1>
                     Statements in the                        Statements in the
      . . .          catch block are             . . .        catch block are
      <c-stmt-m>     executed.                   <c-stmt-m>   skipped.
  }                                          }
                     And the execution
  <next stmt>        continues to the        <next stmt>
                     next statement


Ack: Thomas Wu                                                                 8
  Summary: multiple catch blocks
              Exception                                    No Exception
  try {             Assume <t-stmt-3>          try {
                    throws an exception
      <t-stmt-1>    and <catch-block-3>            <t-stmt-1>
      <t-stmt-2>    is the matching block.         <t-stmt-2>    All statements in
                                                                 the try block are
      <t-stmt-3>                                   <t-stmt-3>
                                                                 executed and throw
      <t-stmt-4>       Remaining                   <t-stmt-4>    no exceptions.
      . . .            statements in the           . . .
                       try block is skipped.
      <t-stmt-n>                                   <t-stmt-n>
  }                                            }
      <catch-block-1>                              <catch-block-1>
      <catch-block-2>         Statements           <catch-block-2>      All catch
                              in the                                    blocks are
      <catch-block-3>                              <catch-block-3>      skipped.
                              matching
      . . .                   catch block          . . .
      <catch-block-m>         are executed.        <catch-block-m>
  }                                            }
      <next stmt>                              <next stmt>

Ack: Thomas Wu                                                                  9
  Summary: with finally block
             Exception                            No Exception
 try {          Assume <t-stmt-i>        try {
                throws an exception
   <t-stmt-1>                              <t-stmt-1>
                and <catch-block-i> is
   . . .        the matching block.        . . .
   <t-stmt-i>                              <t-stmt-i>
   . . .                                   . . .
   <t-stmt-n>                              <t-stmt-n>
 }                                       }
   <catch-block-1>                         <catch-block-1>
   . . .                                   . . .
   <catch-block-i>                         <catch-block-i>
   . . .                                   . . .
   <catch-block-m>                         <catch-block-m>

 } finally {                             } finally {
    . . .            finally block is       . . .        finally block is
 }                   executed.           }               executed.
   <next stmt>                             <next stmt>

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




Ack: Thomas Wu                                                        11
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.




                                               12
    UNIX File Input Redirection
   <:To redirect input from a file
                                      The text file inputFile
      $ java MyProgram < inputFile    contains the input data.




      $ java MySum
      Enter 3 integers: 4 2 3
      Sum = 9

      $ cat sum.in
      4 2 3

      $ java MySum < sum.in
      Enter 3 integers: Sum = 9

                                                             13
    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);
          }
      }
                                                        14
    UNIX File Output Redirection
   >:To redirect output to a file (>>: to append to a
    file)
                                           The text file outputFile
      $ java MyProgram > 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

                                                                  15
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
 Consultation @ my office (COM1-03-12)
   Wednesday (5 Nov) 3 – 6 pm


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




                                                17
End of file




              18

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:3/5/2013
language:English
pages:18