Applets - PowerPoint PPT Presentation

About This Presentation
Title:

Applets

Description:

The caller may not be able to fix it. Problems with return signals ... If the caller can't fix it. We have to punt to the caller's caller. x.doStuff(); // becomes ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 28
Provided by: thaddeusf
Category:
Tags: applets

less

Transcript and Presenter's Notes

Title: Applets


1
Applets Applications
  • CSC 171 FALL 2001
  • LECTURE 15

2
History FORTRAN
  • 1954 - John Backus proposed the development of a
    programming language that would allow uses to
    express their problems in commonly understood
    mathematical formulae -- later to be named FORTRAN

3
Change in Schedule
  • 11/12 Chapter 14 Exception Handling
  • 11/16 Midterm Hoyt 8AM
  • 11/19 Chapters 11, 12, 13
  • Graphics GUIs (optional)
  • Enjoy the Turkey
  • 11/26 Chapter 16 Files Streams
  • 11/29 Project due
  • 12/3 Chapter 19 Data Structures
  • 12/11 last lecture
  • 12/19 FINAL EXAM - 12/19 4PM

4
Exam Friday 11/16
  • 8 AM - 9 AM Hoyt
  • Chapters 1-10
  • end of chapter questions make good study materiel
  • Labs
  • Projects
  • Workshops
  • Multiple choice (20 _at_ 2)
  • Some Write a method/class that . . . (10 _at_ 6)

5
Throwing Exceptions
  • What should a method do when a problem is
    detected?
  • Traditionally, methods return some special code
    to indicate failure
  • However,
  • The caller may forget to check the return value
  • The caller may not be able to fix it

6
Problems with return signals
  • If the caller forgets to check
  • Bad data is processed
  • String input myTextBox.getText()
  • Double d new Double(input) // what if
    I type hello
  • i d.doubleValue()
  • If the caller cant fix it
  • We have to punt to the callers caller
  • x.doStuff() // becomes
  • If (!x.doStuff()) return false //man, thats a
    lot of code

7
Exception-handling mechanism to the rescue
  • Exceptions cant be overlooked
  • Excepts can be handled by a competent handler
  • Not just the caller

8
Simple Try Block Syntax
  • try
  • statement
  • statement
  • catch (ExceptionClass ExceptionObject)
  • statement
  • statement

9
Example Console Input
  • import java.io.
  • public class ReadingDoubles
  • public static void main (String args)
  • double i, j
  • try
  • // open the console for
    bufferd I/O
  • InputStreamReader reader
  • new InputStreamReader(Sys
    tem.in)
  • BufferedReader console
  • new BufferedReader(reader
    )

10
Alternately
  • // instead of
  • InputStreamReader reader
  • new InputStreamReader(Sys
    tem.in)
  • BufferedReader console
  • new BufferedReader(reader
    )
  • // could use nested constructors
  • BufferedReader console
  • new BufferedReader(new
    InputStreamReader(System.in))

11
What happens?
  • public static void main (String args)
  • BufferedReader console
  • new BufferedReader(InputStreamReader(Syste
    m.in))
  • System.out.print("Please enter a floating
    point number ")
  • String input console.readLine()
  • Double d new Double(input)
  • double d1 d.doubleValue()

12
This happens
  • cd d/courses/CSC171/CSC171FALL2001/code/
  • d/devenv/jdk1.3/bin/javac Console1Bad.java
  • Console1Bad.java14 unreported exception
    java.io.IOException must be caught or declared
    to be thrown
  • String input console.readLine() //read a
    String
  • 1 error
  • Compilation exited abnormally with code 1 at Mon
    Nov 12 211452

13
Deal with the potential exception
  • try
  • String input console.readLine()
  • catch (IOException e)
  • System.out.println(e bad read)
  • System.exit(0)

14
Example
  • //get the value
  • System.out.print("Please enter a floating point
    number ")
  • try
  • input console.readLine() //read a String
  • catch (IOException e)
  • System.out.println(e " bad read ")
  • System.exit(0)

15
Now check the number format
  • try
  • Double d new Double(input)
  • double d1 d.doubleValue()
  • // Calculate the sum printout
  • System.out.println(d1 "2 " (d1d1))
  • catch (NumberFormatException e)
  • System.out.println(e
  • " What you entered was not a double")

16
Checked Unchecked Exceptions
  • Java Exceptions fall into two categories
  • Checked
  • You MUST tell the compiler what you are going to
    do about the exception
  • Unchecked (sub-classes of RuntimeException)
  • NumberFormatException
  • IllegalArgumentException
  • NullPointerException

17
Exception Class Hierarchy
18
Why Checked Unchecked?
  • Checked Exceptions are not your fault
  • So, you have to deal with them
  • Unchecked Exceptions are your fault
  • So, we trust you to deal with them
  • You have to deal with things you cannot prevent!

19
Cheap hacks
  • Lazy empty clauses
  • try System.in.read()
  • catch (IOException e)
  • Punt the exception to the caller
  • public static void main(String args) throws
    IOException

20
Alternate version I
  • import java.io.
  • public class ReadingDoubles3
  • public static void main (String args)
    throws IOException
  • InputStreamReader reader new
    InputStreamReader(System.in)
  • BufferedReader console new BufferedReader(reade
    r)
  • //get the first value
  • System.out.print("Please enter a floating point
    number ")
  • String input console.readLine() //read a
    String
  • Double d new Double(input)
  • double d1 d.doubleValue()

21
Alternate Version II
  • // Calculate the sum printout
  • System.out.println(d1 "2 " (d1d1))
  • // wait for user to end
  • System.out.println(" Hit return to exit")
  • System.in.read()

22
Throwing your own exeptions
  • public static double myfun(double
    ValueBetweenZeroAndOne)
  • if ((ValueBetweenZeroAndOne lt 0)
  • ( ValueBetweenZeroAndOne gt 1))
  • throw new IllegalArgumentException("RTFM -
    you dolt!")
  • return ValueBetweenZeroAndOne
    ValueBetweenZeroAndOne

23
Making them deal with it
  • public static double myfun(double
    ValBetZeroAndOne) throws Exception // now, they
    have to write a handler
  • if ((ValBetZeroAndOne lt 0) ( ValBetZeroAndOne
    gt 1))
  • throw new IllegalArgumentException("RTFM you
    dolt!")
  • return ValBetZeroAndOne ValBetZeroAndOne

24
Defining your own exceptions
  • public class DivideByZeroException extends
    RuntimeException
  • public DivideByZeroException()
  • super(Attempt to divide by Zero)
  • public DivideByZeroException( String msg)
  • super(msg)

25
Defining your own checked exceptions
  • public class myCheckException extends Exception
  • public myCheckException()
  • super(Attempt to divide by Zero)
  • public myCheckException( String msg)
  • super(msg)

26
Finally
  • try
  • statements
  • catch (ExceptionClass ExceptionObject)
  • statements //exception specific
  • finally
  • statements //clean up

27
Finally
  • The Finally block executes regardless of what
    happens in the try/catch
  • Good for cleaning up resources
  • Good for conditions where the catch blocks may
    throw exceptions.
Write a Comment
User Comments (0)
About PowerShow.com