Applets & Applications

27
Applets & Applications Applets & Applications CSC 171 FALL 2001 LECTURE 15

description

Applets & Applications. CSC 171 FALL 2001 LECTURE 15. 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. History: FORTRAN. Change in Schedule. - PowerPoint PPT Presentation

Transcript of Applets & Applications

Page 1: Applets & Applications

Applets & ApplicationsApplets & Applications

CSC 171 FALL 2001

LECTURE 15

Page 2: Applets & Applications

History: FORTRANHistory: FORTRAN1954 - 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

Page 3: Applets & Applications

Change in ScheduleChange 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

Page 4: Applets & Applications

Exam Friday 11/16Exam 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 @ 2%) Some “Write a method/class that . . .” (10 @ 6%)

Page 5: Applets & Applications

Throwing ExceptionsThrowing 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

Page 6: Applets & Applications

Problems with return signalsProblems 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 can’t fix it – We have to punt to the caller’s caller

x.doStuff(); // becomes

If (!x.doStuff()) return false; //man, that’s a lot of code

Page 7: Applets & Applications

Exception-handling Exception-handling mechanism to the rescuemechanism to the rescue

Exceptions can’t be overlookedExcepts can be handled by a competent

handler– Not just the caller

Page 8: Applets & Applications

Simple Try Block SyntaxSimple Try Block Syntax

try {statement;statement;

}catch (ExceptionClass ExceptionObject) {

statement;statement;

}

Page 9: Applets & Applications

Example: Console InputExample: 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(System.in); BufferedReader console =

new BufferedReader(reader);

Page 10: Applets & Applications

AlternatelyAlternately// instead of

InputStreamReader reader = new InputStreamReader(System.in);

BufferedReader console = new BufferedReader(reader);

// could use nested constructorsBufferedReader console = new BufferedReader(new InputStreamReader(System.in));

Page 11: Applets & Applications

What happens?What happens?

public static void main (String[] args) { BufferedReader console =

new BufferedReader(InputStreamReader(System.in));

System.out.print("Please enter a floating point number : "); String input = console.readLine(); Double d = new Double(input); double d1 = d.doubleValue(); }

Page 12: Applets & Applications

This happensThis happenscd d:/courses/CSC171/CSC171FALL2001/code/d:/devenv/jdk1.3/bin/javac Console1Bad.javaConsole1Bad.java:14: unreported exception java.io.IOException; must

be caught or declared to be thrownString input = console.readLine(); //read a String

^1 error

Compilation exited abnormally with code 1 at Mon Nov 12 21:14:52

Page 13: Applets & Applications

Deal with the potential Deal with the potential exceptionexception

try { String input = console.readLine(); } catch (IOException e) {

System.out.println(e + “bad read”);System.exit(0);

}

Page 14: Applets & Applications

ExampleExample

//get the valueSystem.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);}

Page 15: Applets & Applications

Now check the number formatNow check the number formattry{

Double d = new Double(input); double d1 = d.doubleValue(); // Calculate the sum & printout System.out.println(d1 + "^2 == " + (d1*d1));}catch (NumberFormatException e) { System.out.println(e +

" : What you entered was not a double");}

Page 16: Applets & Applications

Checked & Unchecked Checked & Unchecked ExceptionsExceptions

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

Page 17: Applets & Applications

Exception Class HierarchyException Class Hierarchy

Page 18: Applets & Applications

Why Checked & Unchecked?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!

Page 19: Applets & Applications

Cheap hacksCheap hacks1. Lazy empty clauses

try {System.in.read()}

catch (IOException e){}

2. Punt the exception to the callerpublic static void main(String[] args) throws

IOException

Page 20: Applets & Applications

Alternate version IAlternate 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(reader);

//get the first valueSystem.out.print("Please enter a floating point number : ");

String input = console.readLine(); //read a StringDouble d = new Double(input); double d1 = d.doubleValue();

Page 21: Applets & Applications

Alternate Version IIAlternate Version II

// Calculate the sum & printoutSystem.out.println(d1 + "^2 == " + (d1*d1));

// wait for user to endSystem.out.println(" Hit return to exit");System.in.read();

}}

Page 22: Applets & Applications

Throwing your own exeptionsThrowing your own exeptions

public static double myfun(double ValueBetweenZeroAndOne) {if ((ValueBetweenZeroAndOne < 0) ||

( ValueBetweenZeroAndOne > 1)) throw new IllegalArgumentException("RTFM - you dolt!");return ValueBetweenZeroAndOne * ValueBetweenZeroAndOne;

}

Page 23: Applets & Applications

Making them deal with itMaking them deal with it

public static double myfun(double ValBetZeroAndOne) throws

Exception { // now, they have to write a handlerif ((ValBetZeroAndOne < 0) || ( ValBetZeroAndOne > 1)) throw new IllegalArgumentException("RTFM – you dolt!");return ValBetZeroAndOne * ValBetZeroAndOne;

}

Page 24: Applets & Applications

Defining your own exceptionsDefining your own exceptions

public class DivideByZeroException extends RuntimeException {

public DivideByZeroException() {super(“Attempt to divide by Zero”);

} public DivideByZeroException( String msg) {

super(msg);}

}

Page 25: Applets & Applications

Defining your own checked Defining your own checked exceptionsexceptions

public class myCheckException extends Exception {public myCheckException() {

super(“Attempt to divide by Zero”);}

public myCheckException( String msg) {super(msg);

}}

Page 26: Applets & Applications

FinallyFinallytry {

statements;}catch (ExceptionClass ExceptionObject) {

statements; //exception specific} finally {

statements; //clean up}

Page 27: Applets & Applications

FinallyFinally

The Finally block executes regardless of what happens in the try/catch

Good for cleaning up resourcesGood for conditions where the catch blocks

may throw exceptions.