Applets & Applications CSC 171 FALL 2001 LECTURE 15.

Slides:



Advertisements
Similar presentations
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
CS102--Object Oriented Programming
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
COMP 121 Week 5: Exceptions and Exception Handling.
Chapter 9 Exception Handling. Chapter Goals To learn how to throw exceptions To be able to design your own exception classes To understand the difference.
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
For use of Cleveland State's IST410 Students only 1 Exception.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
When you use an input or output file that does not exist, what will happen? −The compiler insists that we tell it what the program should do in such case.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Exception Handling. Lecture Objectives To learn how to throw exceptions To be able to design your own exception classes To understand the difference between.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions CIS 304 Intermediate Java Programming for Business.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Java Software Solutions Foundations of Program Design Sixth Edition
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
COMP Exception Handling Yi Hong June 10, 2015.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Chapter 14 Exception Handling. Chapter Goals To learn how to throw exceptions To be able to design your own exception classes To understand the difference.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
MIT AITI 2003 Lecture14 Exceptions
Introduction Exception handling Exception Handles errors
CSE 501N Fall ’09 17: Exception Handling
Exceptions 10-Nov-18.
Advanced Programming Behnam Hatami Fall 2017.
ATS Application Programming: Java Programming
Exception Handling.
Web Design & Development Lecture 7
Chapter 11 Input/Output Exception Handling
Tutorial Exceptions Handling.
Exception Handling Contents
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Applets & Applications CSC 171 FALL 2001 LECTURE 15

History: FORTRAN 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

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

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 2%) Some “Write a method/class that...” 6%)

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

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

Exception-handling mechanism to the rescue Exceptions can’t be overlooked Excepts can be handled by a competent handler – Not just the caller

Simple Try Block Syntax try { statement; } catch ( ExceptionClass ExceptionObject ) { statement; }

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(System.in); BufferedReader console = new BufferedReader(reader);

Alternately // instead of InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader); // could use nested constructors BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

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(); }

This happens cd d:/courses/CSC171/CSC171FALL2001/code/ d:/devenv/jdk1.3/bin/javac Console1Bad.java Console1Bad.java:14: 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 21:14:52

Deal with the potential exception try { String input = console.readLine(); } catch (IOException e) { System.out.println(e + “bad read”); System.exit(0); }

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); }

Now check the number format try{ 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"); }

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

Exception Class Hierarchy

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!

Cheap hacks 1. Lazy empty clauses try {System.in.read()} catch (IOException e){} 2. Punt the exception to the caller public static void main(String[] args) throws IOException

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(reader); //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();

Alternate Version II // Calculate the sum & printout System.out.println(d1 + "^2 == " + (d1*d1)); // wait for user to end System.out.println(" Hit return to exit"); System.in.read(); }

Throwing your own exeptions public static double myfun(double ValueBetweenZeroAndOne) { if ((ValueBetweenZeroAndOne < 0) || ( ValueBetweenZeroAndOne > 1)) throw new IllegalArgumentException("RTFM - you dolt!"); return ValueBetweenZeroAndOne * ValueBetweenZeroAndOne; }

Making them deal with it public static double myfun(double ValBetZeroAndOne) throws Exception { // now, they have to write a handler if ((ValBetZeroAndOne 1)) throw new IllegalArgumentException("RTFM – you dolt!"); return ValBetZeroAndOne * ValBetZeroAndOne; }

Defining your own exceptions public class DivideByZeroException extends RuntimeException { public DivideByZeroException() { super(“Attempt to divide by Zero”); } public DivideByZeroException( String msg) { super(msg); }

Defining your own checked exceptions public class myCheckException extends Exception { public myCheckException() { super(“Attempt to divide by Zero”); } public myCheckException( String msg) { super(msg); }

Finally try { statements; } catch ( ExceptionClass ExceptionObject ) { statements; //exception specific } finally { statements; //clean up }

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.