1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CS102--Object Oriented Programming
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Index Exception handling Exception In Java Exception Types
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
1. 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
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.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
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.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Object Oriented Programming
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Object Oriented Programming with Java (150704).  Throwable Exception (This class will catch exceptions generated by prog.) (Create your own custom exception.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
MCS 270 Spring 2014 Object-Oriented Software Development.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
 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.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Final and Abstract Classes
Java Programming Language
Chapter 12 Exception Handling and Text IO
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
Presentation transcript:

1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10

2 Lecture Review Introduction to Interfaces Implementing and Applying Interfaces Exception Handling Exception Types Statements – try, catch, throw, finally Java’s built-in exceptions

3 Interface Based on the description of Herbert, we can fully abstract a class’s interface from its implementation. That is to say, by use of interface, we can specify what a class must do, but not how it does. Interfaces are syntactically similar to classes, but lack of instance variables.

4 Defining an Interface An interface is defined much like a class. The general form of an interface is: Access interface name { return-type method-name1 (parameter-list) return-type method-name2(parameter-list) …. type final-varname1 = value; …. type final-varnameN = value; }

5 Explanation to the definition Access: it is either public or not used, when it is declared as public, the interface can be used by any other. Name: the name of the interface Variables: it can be declared inside of interface declarations. They are implicitly final and static.

6 An example A simple interface called callback(). Interface Callback { Void callback(int param); }

7 More about Interface (1) - from Clayton Walnum Packages are fairly easy to understand, being little more than a way to organize related classes whether built-in or developed by programmers. Interfaces are a concept that is a bit harder to grasp. To really understand Java's interfaces, you have to know about something called multiple inheritance, which is not allowed in Java, but which is often used in other object-oriented languages.

8 More about Interface (2) - from Clayton Walnum Multiple inheritance means creating a new class that inherits behavior directly from more than one superclass.

9 Summary of Interface An interface is very much like a class-with one important difference. None of the methods declared in an interface are implemented in the interface itself. Instead, these methods must be implemented in any class that uses the interface.

10 Example of Interface

11 Example – result

12 More Example

13 One interface, two implementations

14 Explanation AInterface CClassRClass

15 Interface can be extended One Interface can inherit another by use of the keyword extends. When a class implements an interface that inherits another interface, it must provide implementation for all methods defined within the interface inheritance chain.

16 Example – Herbert, page 246 AB MyClass – 3 methods

17 Exception Handling Mechanism, page 250 An exception is an abnormal condition that happens at run time. It is a run-time error (not compilation error). Some computer languages that do not support the use of error code, the user must check and handle it.

18 Exception Handling in Java A Java exception is an object that describes an exceptional condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. The five keywords are: try, catch, throw, throws and finally.

19 General Form of an exception- handling try { // block of code to monitor for errors } catch (Exception Type1 exOb) { //exception handler for Exception Type 1 } …… finally { // block of code to be executed before try block ends}

20 An example – uncaught exceptions Look at the following example, without proper handling, it will cause an error of divide-by-zero. Class divide { public static void main(String args[]) { int d = 0; int a = 12/d; } } Divide by 0

21 Using try and catch Although the default exception handler provided by the java run-time system is useful, we will usually want to handle exception. There are two benefits: 1) It allows us to fix the error 2) it prevents program from automatically terminating.

22 Example – try and catch, divide by zero

23 Explanation Note that the call to println(“This will not be printed..”) inside the try block is never executed. Once an exception is thrown, program control transfers out of the try block into the catch block. That is to day, once there is an arithmetic error, it will exit from the try block to the catch block

24 Example – try and catch, not divide by zero, note there is no error

25 Example – a loop is used with two random integers. These two integers are divided by each other and the result is used to divide the value 11.

26 Multiple Catch Clauses In some cases, more than one exception could be raised by a single piece of code. To handle this, we can specify two or more catch clauses. Each of them catches a different type of exception.

27 Example of multiple catch

28 Explanation When there is no argument, args.length becomes 0 and b = 12/a becomes an error of “divide by 0” When there is an argument, args.length becomes 1, the program executes c[12] which is not defined. The program defines c[] = {1}, one element only.

29 Nested try Statements The try statement can be nested. A try statement can be inside the block of another try. Each time a try statement is entered, the context will be put on a stack (a temporary location.)

30 Example

31 throw It is possible for our program to throw an exception explicitly using the throw. The general form is throw throwableitem; Here, throwableitem must be an object of type that can be throwable or a subclass of throwable.

32 Example

33 Explanation The program has two chances to deal with the same error. First, main() sets up an exception context and then calls demoproc(). The demoproc() method then sets up another exception-handling context and immediately throws a new instance of NullPointerException.

34 finally finally creates a block o code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown.

35 Example

36 Summary Introduction to Interfaces - we an specify what a class must do, but not how it does. Implementing and Applying Interfaces – interface (defines) others (implement, methods) Exception Handling – abnormal case, the general form is try {….} catch{….} Exception Statements – try, catch, throw, finally