Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter.
CS102--Object Oriented Programming
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
COMP 121 Week 5: Exceptions and Exception Handling.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
COMP Exception Handling Yi Hong June 10, 2015.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
Question of the Day  A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Chapter 8-Exception Handling/ Robust Programming.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
CSE 501N Fall ’09 17: Exception Handling
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Exceptions 10-Nov-18.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Quiz 1 marking nears completion!
Exception Handling.
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CMPE212 – Reminders Assignment 3 due next Friday.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Exceptions 10-May-19.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public | private –static

Winter 2006CISC121 - Prof. McLeod2 Assignment 1 & “Stuff” If you are having problems with assn1, have a look at the solution to Lab 3. Class rep (for USAT’s): –please contact Mireille Gomes at

Winter 2006CISC121 - Prof. McLeod3 Today Why write Methods? Passing parameters by value or by reference. Exceptions.

Winter 2006CISC121 - Prof. McLeod4 Method Examples public void printHello() { System.out.println(“Hello”); } // end printHello public void printHelloName(String yourName) { System.out.println(“Hello “ + yourName); } // end printHelloName public void printAvg(int a, int b) { System.out.println((a + b) / 2.0); } // end printAvg

Winter 2006CISC121 - Prof. McLeod5 Method Examples - Cont. public double average(double a, double b) { return (a + b) / 2; } // end average public int lowest(int a, int b) { if (a <= b) return a; else return b; } // end lowest

Winter 2006CISC121 - Prof. McLeod6 Methods - Cont. Why use methods? Modular design. Avoids repetitious code. Independent testing of sub-tasks. Reusable code. –Design and test a method once, and re-use it whenever you need to solve a similar problem. Isolation from unintended side effects. –The only variables from the caller that can be seen from a method are those in the parameter list.

Winter 2006CISC121 - Prof. McLeod7 Methods - Cont. Start thinking of your program as a collection of methods, where the main method only acts to call the other methods. The use of modular design techniques with methods is called “encapsulation”.

Winter 2006CISC121 - Prof. McLeod8 Methods – Some Advice… When designing method operations, make sure a method only does one “thing”, do not combine operations (except in main ). Concentrate on coding the individual methods, one at a time, when you write a program. If you have thought out the “code-level interface” between the methods, then the methods will be completely independent of each other. Try to minimize the size of the main method, if possible. However, if the main method is going to be responsible for screen I/O then more code may be necessary.

Winter 2006CISC121 - Prof. McLeod9 Passing Parameters by Reference See PassingDemo.java –Has a method with two parameters - an array and an int - which one(s) will stay changed? –Instead of going element by element, if you re-assign the array to another array within the method, what happens?

Winter 2006CISC121 - Prof. McLeod10 Passing Arrays by Reference Summary of PassingDemo.java: –Primitive types are passed by value. –Only element by element changes in arrays will “stick”. –Re-assigning the array to a pointer that has local scope in a method will not “stick”.

Winter 2006CISC121 - Prof. McLeod11 Exceptions How can a method indicate that it is unable to return what it is supposed to return? How can a method deliver details about the error condition? How can you prevent the instantiation of an Object? The limitation of only returning a single “thing” means that you either designate error values for the “thing” or you have some other way to return the indication of an error.

Winter 2006CISC121 - Prof. McLeod12 Exceptions - Cont. The designers of Java followed conventions used by many other OOP languages, they allowed for another way to return something. Exceptions are objects (big surprise!). When an error condition is encountered, a method can throw an instance of a pre-defined exception object.

Winter 2006CISC121 - Prof. McLeod13 Exceptions - Cont. How does an Exception Object contain information about the error condition? –The name of the Object: IOException NumberFormatException FileNotFoundException ArrayIndexOutOfBoundsException … So, the method must throw a relevant exception object. Turns out that exceptions can also carry a String message.

Winter 2006CISC121 - Prof. McLeod14 Exceptions – Cont. An exception is just a: The exception itself cannot do anything about the problem.

Winter 2006CISC121 - Prof. McLeod15 Exceptions – Cont. The act of throwing an exception immediately stops the method that is throwing the exception (like a return statement…). Execution control is passed back to the invoking method, that was possibly waiting for a return value! No return value is forthcoming!

Winter 2006CISC121 - Prof. McLeod16 Exceptions - Cont. Instead the invoking method must be prepared to catch the exception. This is done with a “try/catch” block:

Winter 2006CISC121 - Prof. McLeod17 Exceptions – Cont. Syntax of a “try-catch block”: try { // block of statements that might // generate an exception } catch (exception_type identifer) { // block of statements }[ catch (exception_type identifer) { // block of statements … }][ finally { // block of statements }]

Winter 2006CISC121 - Prof. McLeod18 Exceptions – Cont. You must have at least one “catch block” after the “try block” (otherwise the try block would be useless!) You can have many catch blocks, one for each exception you are trying to catch. The code in the “finally” block is always executed, whether an exception is thrown, caught, or not.

Winter 2006CISC121 - Prof. McLeod19 Exceptions - Cont. A method can throw more than one kind of exception. Why would you want to do this? You can also include more than one line of code that can throw an exception inside a try block. Why would you not want to do this?

Winter 2006CISC121 - Prof. McLeod20 Exceptions – Cont. We have had to use try/catch blocks with file I/O - the nasty compiler forces us to do so! We can’t ignore the possibility of certain methods throwing exceptions: FileReader fileIn = null; try{ fileIn = new FileReader(filename); } catch (FileNotFoundException e) { System.out.println("Cannot open file!"); System.exit(0); } Scanner fileInput = new Scanner(fileIn);

Winter 2006CISC121 - Prof. McLeod21 Exceptions – Cont. Note that you can use the code e.getMessage() to get an error description string from the exception inside the catch clause. You can supply the message to the Exception constructor when you throw one (later). When used with some exceptions, the e.getMessage() method just returns null, as they do not contain a message.

Winter 2006CISC121 - Prof. McLeod22 Exceptions – Cont. What exception classes to use? –The pre-compiler in eclipse will tell you which exceptions you have to catch. –Observe the error at runtime, and note the exception name. –Look at the Exception class hierarchy in the API documentation. –A more abstract exception class will catch more exceptions with a single “catch” clause, but the more abstract the error condition, the harder it will be to report and/or deal with on concrete terms.

Winter 2006CISC121 - Prof. McLeod23 Exceptions – Cont. If an exception is thrown and not caught? –Exception was thrown outside of a try-catch block, or –Was inside of a try block, but no catch statement matched the exception: Now the exception is “propogated” back to the calling method. If the calling method called the original method in a try block, then the calling method tries to catch the exception.

Winter 2006CISC121 - Prof. McLeod24 Exceptions – Cont. Otherwise the exception just keeps moving up the “call chain”. If not caught, eventually it will reach the main method. If main does not catch the exception, the program terminates (“crashes!”) and a cryptic method is sent to the screen, identifying the exception.

Winter 2006CISC121 - Prof. McLeod25 Defining Exceptions You can throw an exception already defined in java, but: Most of the time you will want to throw your own exception objects. Why not? It’s easy! See the next slide for the definition of an Exception object.

Winter 2006CISC121 - Prof. McLeod26 Defining Exceptions, Cont. public class IllegalStudentInfoException extends Exception { public IllegalStudentInfoException (String message) { super(message); } public IllegalStudentInfoException () { super("Illegal data supplied to constructor."); } } // end IllegalStudentInfoException

Winter 2006CISC121 - Prof. McLeod27 Defining Exceptions, Cont. Inside a method that detects this condition: throw new IllegalStudentInfoException("Illegal student number: " + number); At the end of the method header that contains the above line of code: … throws IllegalStudentInfoException {

Winter 2006CISC121 - Prof. McLeod28 Defining Exceptions, Cont. This example contains a few Java syntax elements that we have not yet discussed: –extends –super –throw –throws We may not discuss the exact meaning and usage of these keywords – but you can still write your own exceptions by following the provided pattern!