OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Yoshi
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
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.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
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 in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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.
CS203 Java Object Oriented Programming Errors and Exception Handling.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
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.
Slides Credit Umair Javed LUMS Web Application Development.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Exceptions and assertions CSE 331 University of Washington.
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.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
1 Features of Java (2) CS 3331 Sections 4.5 and 4.6.
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.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
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.
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.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
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.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Generics, Exceptions and Undo Command
Tirgul 13 Exceptions 1.
Chapter 13 Exception Handling
Exceptions 10-Nov-18.
Exceptions 19-Feb-19.
Java Exceptions Dan Fleck CS211.
Exceptions 7-Apr-19.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Exceptions 10-May-19.
Java Basics Exception Handling.
Exceptions 5-Jul-19.
Presentation transcript:

OOP Tirgul 7

What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2

Packages

 Packages allow you to group together related classes, like “folders” for classes.  Packages make it easier to:  Be aware of all the classes relating to the same purpose.  Solve conflicting names.  Additionally, packages play a role in access permission between classes.

Creating packages  The first line of every source file (*.java) should start with a package statement (one statement in each file). package package.subpackage;

Creating packages  Two ways of using code from another package:  Importing a Package Member import vehicle.Car;  Importing an Entire Package import vehicle.*;

Grouping related classes Vehicle CarTrainPlaneBicycleBike

Grouping related classes Now the user can easily know which Vehicle implementations are available.

Solving name conflicts  Let’s say you’ve got two classes:  Driver: Represents a vehicle driver.  Driver: Represents a computer driver.  We’ll get a compilation error if we’d try to compile both classes in the “same package”.  This, however, will work:

Solving name conflicts Will this work?

Solving name conflicts To distinguish between the two we must use the fully qualified name.

Solving name conflicts

Packages: Permissions  Do you remember the protected access modifier?  Protected fields can be accessed by:  Subclasses.  All the classes that share the same package.

Packages: Permissions

Compilation Error!

Packages: Permissions  What if there is no modifier? No modifier

Exceptions

 Exceptions are messages that state that something went wrong.  Exceptions are implemented as Java objects.  As such, fields and methods play a different role in exception classes:  Usually, the calling class knows what the message is by the specific exception class.  Each type of message has its own class.

Exceptions  Sometimes an exception object will specify more information (usually for debug purposes) such as a string specifying what exactly went wrong.

Motivation What if this line fails? What if this line fails?? What if this line fails??? What if this line fails???? What if this line fails?????

Motivation

 Reason #1 to use exceptions: Separating Error-Handling Code from "Regular" Code

Exception class hierarchy Throwable ExceptionError checked unchecked RunTimeException

Exception classes  Use existing exceptions when appropriate.  New exception classes that we write will almost always be checked;  I.e., extend the Exception class.  Must be caught or declared as thrown.  Exception classes that we write may have their own hierarchay;  Allows treating several types of errors similarly.

Unchecked exceptions  Represent problems that are the result of a programming problem:  Arithmetic exceptions (dividing by zero)  Pointer exceptions (accessing a null reference)  Indexing exceptions (accessing an array element with an out-of-bounds index)  Calling code cannot be expected to recover or handle them.

Defining new Exception classes Out of milk Class Coffee Vending Machine Out of coffee Out of sugar getDrink() getCoffee() getLatte()

Defining new Exception classes public class LatteException extends Exception{ private static final long serialVersionUID = 1L; public LatteException () { super(“Problem with ordering coffee”); } public LatteException (String s){ super(s); } Default Ctor Message Ctor What’s this? At the moment: 1.Write this (“1L”) in every Exception class. 2.Prevents an annoying warning. 3.We’ll learn about it in week 12.

Throwing Exceptions Latte getLatte() throws LatteException { if (milk.isEmpty() || suagr.isEmpty()) throw new LatteException (); }

Using Exception Hierarchy Exception LatteException OutOfMilkException OutOfSugarException

Using Exception Hierarchy class LatteException extends Exception{…} class OutOfMilkException extends LatteException {…} class OutOfSugarException extends LatteException {…} private Latte getLatte() throws LatteException { if (milk.isEmpty()) throw new OutOfMilkException (); if (suagr.isEmpty()) throw new OutOfSugarException (); //... } Why throw an exception instead of handling it? 1.It’s unexpected (for this method, anyway). 2.It’s rare. 3.Only some specific calling function should and/or could handle this exception.

Catching Exceptions Public getCoffee(){ try{ // Making Latte Latte l = getLatte(); //... } catch(LatteException e){ checkLatteSystem(); } getDrink() getCoffee() getLatte()

Catching Exceptions Public getCoffee(){ try{ // Making Latte Latte l = getLatte(); //... } catch(OutOfMilkException e){ orderMilk(); } catch(LatteException e){ handleGeneralLatteExceptions(); { } getDrink() getCoffee() getLatte()

Motivation  Reason #2 to use exceptions: Grouping and Differentiating Error Types

try{ // Making Latte Latte l = getLatte(); //... } catch(LatteException e){ //... } catch(OutOfMilkException e){ //... } What’s Wrong Here? Compilation Error! OutOfMilkException OutOfSugarException LatteException

Exception Declaration  Each method must declare the checked exceptions it throws. public Latte getLatte() throws LatteException { if (milk.isEmpty()) throw new OutOfMilkException (); if (suagr.isEmpty()) throw new OutOfSugarException (); //... }

Exception Flow Out of milk Class Coffee Vending Machine Out of coffee Out of sugar getDrink() getCoffee() getLatte()

Exception Declaration  “Translating” Exceptions from one class to another through encapsulation. try{ //call some Latte method }catch(LatteException e){ throw new BadDrinkException("problem", e); } Latte Coffee e1 e2 Call 1 Call 2 Drink Preserving the full stack trace

Motivation  Reason #3 to use exceptions: Propagating Errors Up the Call Stack

Handling several types of Exceptions  If we want to catch more than one exception but handle them all in the same way, we can use the “|” operator try { // Making Latte Latte l = getLatte(); //... }catch(Exception1|Exception2 e){ // Handle Exception1, Exception2 in the same way e.getMessage(); }

Exceptions and Packages  Exceptions should be put in the same package as the classes that throws them  And not all in the same package  IOException resides in java.io  EmptyStackException resides in java.util  This is because exceptions are logically connected to the classes that throw them  More than they are connected to one another

Bad usage of Exception Handling  Using a computer language's error handling structures to perform normal program logic. try { int idx = 0; while (true) { displayProductInfo(prodnums[idx]); idx++; } } catch (IndexOutOfBoundsException e) { // Do some cleanup } Heavy! Unexpected! Hides bugs!

Ex4

 You will implement an AVL Tree, according to some API we will give you.  You don’t get any code from us.  A great way to make sure you not only understand some specific DaSt subject, but also know how to implement a theoretical data structure.