Exceptions - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Exceptions

Description:

Example: try { System.out.println(5/x); } catch(Exception e) ... f.println(...); // use like System.out. Make sure to close the file before exiting the program ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 19
Provided by: andrewbv
Category:
Tags: exceptions | make

less

Transcript and Presenter's Notes

Title: Exceptions


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
Built-in Exceptions(Unchecked RuntimeException
Subclasses)
9
Built-in Exceptions(Unchecked RuntimeException
Subclasses)
10
Files
11
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

12
Input/Output Classesin 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

13
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

14
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.tx
    t))
  • Use read methods to read from file
  • s f.readLine() // reads a string

15
Exceptions
  • File operations throw exceptions so make sure
    statements are enclosed in a try-catch statement
  • Exceptions thrown
  • IOException
  • Common (specific) exception FileNotFoundExceptio
    n

16
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.math.admu.edu.ph)

17
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

18
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
Write a Comment
User Comments (0)
About PowerShow.com