Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCSS 143, Autumn 2004 Lecture Notes

Similar presentations


Presentation on theme: "TCSS 143, Autumn 2004 Lecture Notes"— Presentation transcript:

1 TCSS 143, Autumn 2004 Lecture Notes
Error-handling with Exceptions; Input and Output with Streams

2 Input and output, in brief
relevant classes are in the java.io package (must import java.io.*; in your program) some classes of interest: File: representation of a file on the hard disk (not its contents per se, but its name and path) public File(String filename) Scanner has a constructor that has a File argument FileInputStream: a source to read characters from a file FileOutputStream: a target to write bytes into a file (We'll come back to Java's I/O soon...)

3 A Scanner to read a file File file = new File("input.txt");
Scanner in = new Scanner(file); while (in.hasNextInt()) System.out.println(in.nextInt()); ReadFile.java:6: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown ^ 1 error does not compile because the programmer hasn't specified what to do if the file isn't found

4 How C does error-handling
every important function returns an error code that indicates whether or not it failed leads to code that constantly checks for errors with tons of if statements (this is a pain!) // open socket if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) die("Cannot create socket"); sender_addr.sin_family = AF_INET; if (bind(sock, (struct sockaddr*)&sender_addr, sizeof(struct sockaddr_in)) < 0) { close(sock); die("Cannot bind socket: port in use"); }

5 A better approach to errors
it would be more desirable to specify one or more error handling behaviors for a larger range of code, like so: RUN THE FOLLOWING CODE { // open socket sock = socket(PF_INET, SOCK_DGRAM, 0); sender_addr.sin_family = AF_INET; bind(sock, (struct sockaddr*)&sender_addr, sizeof(struct sockaddr_in)); close(sock); } AND IF ANY ERROR OCCURS, DO THIS { die("Cannot create/bind socket: port in use");

6 Java's error-handling: Exceptions
exception: an object that represents an (possibly fixable) unexpected error in a program throw: to create an exception object to announce an error in a running program by default, this will halt the execution of the program catch: to detect and handle an exception that occurs in a running program catching an exception will prevent it from halting the program allows for non-local error handling (one piece of code generates the error; another deals with it)

7 The Exception class an exception has a message about what went wrong, and a stack trace of the methods that were running when it occurred public Exception() Constructs a new exception with null as its detail message. public Exception(String message) Constructs a new exception with the specified detail message. public String getMessage() Returns the detail message string of this exception. public void printStackTrace() Prints this exception and its method call trace to the standard error stream. public String toString() Returns a short description of this exception.

8 Exception class hierarchy (partial)
Java exceptions come in many types that indicate more specifically what went wrong example: IOException means an input or output failed errors in Java form an inheritance hierarchy Throwable Error OutOfMemoryError, StackOverflowError Exception IOException FileNotFoundException, MalformedURLException, SocketException RuntimeException ArithmeticException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException, NullPointerException, UnsupportedOperationException

9 Exceptions vs. errors in Java
Exceptions (can be handled by program): I/O errors (keyboard / floppy / disk / input) network errors (internet, LAN) illegal casting, object dereferencing (null), math array / collection index out of bounds Errors (cannot be handled reasonably by program): computer out of memory Java Virtual Machine bug / error / crash corrupt Java classes required by program infinite method calls crash Java call stack we will deal with Exceptions only, because nothing in general can be done to recover from a Java Error

10 Throwing exceptions an exception may be thrown at any point in code, at programmer's discretion, to halt the program's execution format: throw new ExceptionType (arguments ); most Exception types have a () and a (String) constructor Scanner in = new Scanner(System.in); System.out.print("Enter a positive number: "); int num = in.nextInt(); if (num < 0) throw new RuntimeException("bad number!"); Enter a positive number: -17 java.lang.RuntimeException: bad number at TestProgram.main(TestProgram.java:17)

11 Exceptions in classes Often a method of an object will throw exceptions if certain conditions are violated: the method is passed invalid arguments the method is called when the object is not in an appropriate state to run the method example: removal from an empty linked list a throw statement is like a return statement; it exits the method immediately (and possibly exits the entire program!) // in IntLinkedList class public int removeFirst() { if (isEmpty()) throw new NoSuchElementException( "empty list"); }

12 Causing an exception to occur
Scanner's nextInt method throws a NoSuchElementException when it is asked for a next element of the wrong type Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); int num = in.nextInt(); System.out.println("number typed: " + num); Enter a number: Marty java.util.NoSuchElementException at MyClass.main(MyClass.java:26)

13 Handling an exception to prevent an exception from halting a Java program, you must write code to explain what to do if the exception occurs 'try' to execute the code, and if an exception occurs, 'catch' it by executing some code to deal with it try { // code that might generate an exception statement(s); } catch (ExceptionType exceptionVariableName) { // code to deal with the exception }

14 Catching an exception The previous code, now handling the exception:
Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); try { int num = in.nextInt(); System.out.println("number typed: " + num); } catch (NoSuchElementException e) { System.out.println("You didn't type a number!"); } Enter a number: Marty You didn't type a number! Could we have avoided the exception in the first place?

15 Multiple catch blocks If one block of code could generate several types of exceptions, its try block can have several catch blocks (one to handle each) try { // code that might generate these exceptions statement(s); } catch (ExceptionType1 exceptionVariableName1) { } catch (ExceptionType2 exceptionVariableName2) { } catch (ExceptionTypeN exceptionVariableNameN) { }

16 Catching and inheritance
since exceptions are in an inheritance hierarchy, catching ExceptionType will catch that type or any subclass of that exception type to catch any exception, use catch Exception might this be a bad idea? should catch the more specific exception type if possible try { statement(s); } catch (Exception e) { }

17 Exceptions and the Java API
many methods in Java's library classes have methods that might throw exceptions some of these types of exceptions are listed in the API next to the method's name example: public Scanner(File file) throws FileNotFoundException if a method lists such an exception, the Java compiler will not allow you to call this method / constructor without explicitly describing what you will do to repair the error with try/catch Java decided it was important enough to force your code to check for these kinds of program errors, or else force you to be explicit if you choose to ignore them this is called a 'checked exception' one notable example: IOException (and its subclasses)

18 Revisited: Scanner to read file
this is the code from the start of these slides, modified so that it now compiles successfully File file = new File("input.txt"); try { Scanner in = new Scanner(file); while (in.hasNextInt()) System.out.println(in.nextInt()); } catch (FileNotFoundException e) { System.out.println( "File input.txt was not found!"); System.exit(0); }

19 Re-throwing an exception
this is the code from the start of these slides, modified so that it explicitly doesn't handle the exception now, the FileNotFoundException behaves like runtime exceptions; if it occurs, no code is in place to handle it, so it will dump its text to the console onscreen public static void main(String[] args) throws FileNotFoundException { File file = new File("input.txt"); Scanner in = new Scanner(file); while (in.hasNextInt()) // I hope it works! System.out.println(in.nextInt()); }

20 Good Ways to Handle Exceptions
print an error message (S.O.println) pop up an error window (in GUI programs) re-prompt the user (for keyboard errors) try the operation again (for I/O problems) fix / correct the error yourself (not always possible) re-throw it (if you shouldn't handle it, but perhaps someone else should, such as the method who called you)

21 BAD Exception Handling
tiny try block (micro-management) huge try block over-general catch clause (catch Exception) ineffectual catch body (e.g. {}) catching a runtime exception where it could have been prevented by simple checking (e.g. null, index)

22 BAD Exception handling code
this code compiles, but it's impossible to tell when something goes wrong (note the empty catch block) File file = new File("input.txt"); try { Scanner in = new Scanner(file); while (in.hasNextInt()) System.out.println(in.nextInt()); } catch (Exception e) {}

23 Checked vs. runtime exceptions
checked exceptions: could have been caused by something out of your program's control; MUST be dealt with (or explicitly re-thrown) by your code, or else the program will not compile unchecked (runtime) exceptions: errors that probably could have been avoided by "looking before you leap" in your code (testing for appropriate problems ahead of time in the code) need not be handled, but will crash the program if a non-handled runtime exception is thrown in Java, these are any exception type that extends the RuntimeException type

24 Throwing checked exceptions
you can throw checked exceptions from your code, but: your method header must specify all types of checked exceptions that it throws with a throws ExceptionType clause on its header anyone who calls the "throwing" method must now handle its checked exceptions by wrapping their call to your method in a try/catch block, or must re-throw them (write a similar throws clause on their method's header) public static String readFileToEnd(String fileName) throws IOException { Scanner in = new Scanner(fileName); String result = ""; while (in.hasNextLine()) result += in.nextLine() + '\n'; return result; }

25 Creating your own exceptions
It is possible to create your own exception types, specific to your application and code public class VehicleException extends RuntimeException { public VehicleException() { super("Error in movement of vehicle"); public VehicleException(String message) { super(message); } Why might one want to do this? Why not use an existing type?

26 Some Points to Note you cannot catch an Error--why?
checked exceptions are a nuisance checked exceptions occur all over the place in these parts of Java: input and output (I/O) networking / internet remote code invocation reflection concurrent / multi-threaded programming

27 Practice problems Add exception-handling code to the IntArrayList or IntLinkedList classes we wrote, to throw exceptions when bad indexes are passed or on removals from an empty list. Add exception-handling code to BankAccount to throw exceptions on bad IDs. Add code to read two numbers from the user and divide the first number by the second. Catch the exception when the user doesn't enter valid numbers, and the exception for when the division fails.

28 Input and output streams
stream: an abstraction of a source or target of data bytes "flow" to (output) and from (input) streams can represent many data sources: files on hard disk another computer on network web page input device (keyboard, mouse, etc.)

29 Stream classes in Java Java has classes InputStream and OutputStream to represent a common superclass with the same methods to read/write data to many different sources console, file, network connection, web page java.io.InputStream FileInputStream, ObjectInputStream, ZipInputStream... java.io.OutputStream FileOutputStream, PrintStream, ObjectOutputStream...

30 The most famous streams
in the java.lang.System class, public static InputStream in public static PrintStream out right now, we know how to: print to the console (System.out.println) read from the console (new Scanner(System.in)) read from a file (new Scanner(new File(filename)) how do we print to a file?

31 PrintStream objects System.out is an object of type java.io.PrintStream PrintStream has this constructor: public PrintStream(OutputStream out) we can also get a PrintStream that refers to a given file, and then we can println into that file when done using a PrintStream into a file, we must close it to make sure the data gets written correctly FileOutputStream file = new FileOutputStream("output.txt"); PrintStream out = new PrintStream(file); out.println("hello, world!"); out.close();

32 PrintStream example // re-writes the given file with every line indented // by one tab character. public static void indentFile(String filename) throws IOException { // first, read and indent the contents of the file Scanner in = new Scanner(new File(filename)); String text = ""; while (in.hasNextLine()) text += '\t' + in.nextLine() + '\n'; in.close(); // should close the scanner in this case // now, output the indented text to the same file FileOutputStream file = new FileOutputStream(filename); PrintStream out = new PrintStream(file); out.print(text); out.close(); }

33 Extra: reading from the web
a Scanner will read input from any Stream source so far, we have used the console input stream (System.in), and files we can also tell the Scanner to read data from a Web page by opening an input stream to that page's URL address class java.net.URL public URL(String address) throws MalformedURLException public InputStream openStream() throws IOException

34 Reading web page example
import java.io.*; import java.net.*; import java.util.*; // Reads and prints a web page's text. public class ReadWebPage { public static void main(String[] args) { try { URL url = new URL(" Scanner in = new Scanner(url.openStream()); while (in.hasNextLine()) System.out.println(in.nextLine()); } catch (MalformedURLException e) { System.out.println("Unable to read the web page."); } catch (IOException e) { System.out.println("Error during input/output."); }

35 I/O practice problems Write a static method reverseFile that reads a given file name and re-writes the file with its text reversed. Write a static method printWebPageStats that reads a given web page address and prints out how many characters, words, and lines are in the web page.

36 Serialization reading / writing objects and their exact state using streams allows objects themselves to be written to files, across network, to internet, etc. avoids converting object’s state into arbitrary text format

37 Classes used for serialization
java.io.ObjectInputStream public ObjectInputStream(InputStream in) public Object readObject() throws ... (many!) java.io.ObjectOutputStream public ObjectOutputStream(OutputStream out) public void writeObject(Object o) throws IOException

38 Serialization Code throws IOException {
public static void writeObjectToFile(Object obj, String filename) throws IOException { FileOutputStream file = new FileOutputStream(filename); ObjectOutputStream oos = new ObjectOutputStream(file); oos.writeObject(obj); os.close(); } public static Object readObjectFromFile(String filename) throws Exception { FileInputStream file = new FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(file); Object obj = ois.readObject(); is.close(); return obj;

39 Making Your Classes Serializable
must implement the (methodless) java.io.Serializable interface for your class to be compatible with object input/output streams don't have to write any methods; implementing the interface is just like a flag to tell Java that it's okay to save your objects to files public class BankAccount implements Serializable { ... ensure that all instance variables inside your class are either serializable as well (all primitives, strings, all collections, and most standard types are) GUI classes, network code, Scanners aren't

40 References Koffman/Wolfgang Ch. 2, pp. 61-85
The Java Tutorial: Exception-Handling. nutsandbolts/exception.html The Java Tutorial: Handling Errors with Exceptions. exceptions/index.html The Java Tutorial: I/O. essential/io/index.html


Download ppt "TCSS 143, Autumn 2004 Lecture Notes"

Similar presentations


Ads by Google