Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.

Similar presentations


Presentation on theme: "Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that."— Presentation transcript:

1 Exceptions

2 Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that does not exist Java provides a way to handle exceptions that are thrown: the try-catch statement

3 Try-catch Statement Syntax: try { … } catch(Exception e) {... } Example: try { System.out.println(5/x); } catch(Exception e) { System.out.println(“/ by zero”); }

4 Breaking out of the try Block try { statement1; statement2; // if exception occurs here, // statement3 will be skipped statement3; } catch(Exception e) { statement4; // executed after exception occurs }

5 Why Use Try-catch ? Alternative: if-statement will be complex and hard to read if there are several exceptions what if the exception occurs within a loop ? (will need to worry about breaking out of the loop) Using try-catch is a more robust and structured way of handling exceptions

6 Exception Classes Classes that extend the Exception class Allows the programmer to be more specific about the exception: try { … } catch (ArithmeticException e) { … } Useful in a try-catch chain

7 Try-catch Chain try { … } catch(SomeException se) { … } catch(AnotherException ae) { … } catch(YetAnotherException yae) { … } …

8 Files

9 File Unit of secondary storage Stores a sequence of bytes/characters Stream operations: read from stream, write to stream Associated with a filename Often organized under a directory hierarchy

10 Input/Output Classes in Java I/O viewed as a stream of bytes parent classes: InputStream, OutputStream As a stream of (Unicode) characters parent classes: Reader, Writer Need to import java.io.*; * An application employing files will use a subclass of one of the above classes

11 Text Files To create a text file, use PrintStream f = new PrintStream(new FileOutputStream(“filename.txt”)); To write to the text file use print methods f.println(…); // use like System.out Make sure to close the file before exiting the program f.close(); // ensures contents are updated

12 Text Files, continued To read from text files, use either DataInputStream or BufferedReader f = new DataInputStream( FileInputStream(“filename.txt”)); f = new BufferedReader(new FileReader(“filename.txt”)); Use read methods to read from file s = f.readLine(); // reads a string

13 Exceptions File operations throw exceptions so make sure statements are enclosed in a try-catch statement Exceptions thrown: IOException Common (specific) exception: FileNotFoundException

14 Reading a File from the Web Use URL class from java.net To open, wpage = new URL(address); f = new BufferedReader(new InputStreamReader(wpage.openStream())) ); * address is a String specifying the webpage address (e.g., “http://www.admu.edu.ph”)

15 Binary Files Text files are often sufficient but sometimes we want to store objects as they are (not their text forms) in a file Use ObjectOutputStream and ObjectInputStream operations: writeObject() and readObject() common technique: store objects in a Vector and then save the Vector in the file

16 java.io.* Summary There is a host of classes under this package that serve a variety of purposes Hints: use “javap java.io.classname” to find out available constructors and methods you often need to use FileInputStream, FileOutputStream, FileReader, and FileWriter to associate a name to the file


Download ppt "Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that."

Similar presentations


Ads by Google