Download presentation
Presentation is loading. Please wait.
Published byBernadette McDaniel Modified over 8 years ago
1
OOP Tirgul 7
2
What We’ll Be Seeing Today Packages Exceptions Ex4 2
3
Packages
4
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.
5
Creating packages The first line of every source file (*.java) should start with a package statement (one statement in each file). package package.subpackage;
6
Creating packages Two ways of using code from another package: Importing a Package Member import vehicle.Car; Importing an Entire Package import vehicle.*;
7
Grouping related classes Vehicle CarTrainPlaneBicycleBike
8
Grouping related classes Now the user can easily know which Vehicle implementations are available.
9
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:
10
Solving name conflicts Will this work?
11
Solving name conflicts To distinguish between the two we must use the fully qualified name.
12
Solving name conflicts
13
Packages: Permissions Do you remember the protected access modifier? Protected fields can be accessed by: Subclasses. All the classes that share the same package.
14
Packages: Permissions
15
Compilation Error!
16
Packages: Permissions What if there is no modifier? No modifier
17
Exceptions
18
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.
19
Exceptions Sometimes an exception object will specify more information (usually for debug purposes) such as a string specifying what exactly went wrong.
20
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?????
21
Motivation
22
Reason #1 to use exceptions: Separating Error-Handling Code from "Regular" Code
23
Exception class hierarchy Throwable ExceptionError checked unchecked RunTimeException
24
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.
25
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.
26
Defining new Exception classes Out of milk Class Coffee Vending Machine Out of coffee Out of sugar getDrink() getCoffee() getLatte()
27
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.
28
Throwing Exceptions Latte getLatte() throws LatteException { if (milk.isEmpty() || suagr.isEmpty()) throw new LatteException (); }
29
Using Exception Hierarchy Exception LatteException OutOfMilkException OutOfSugarException
30
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.
31
Catching Exceptions Public getCoffee(){ try{ // Making Latte Latte l = getLatte(); //... } catch(LatteException e){ checkLatteSystem(); } getDrink() getCoffee() getLatte()
32
Catching Exceptions Public getCoffee(){ try{ // Making Latte Latte l = getLatte(); //... } catch(OutOfMilkException e){ orderMilk(); } catch(LatteException e){ handleGeneralLatteExceptions(); { } getDrink() getCoffee() getLatte()
33
Motivation Reason #2 to use exceptions: Grouping and Differentiating Error Types
34
try{ // Making Latte Latte l = getLatte(); //... } catch(LatteException e){ //... } catch(OutOfMilkException e){ //... } What’s Wrong Here? Compilation Error! OutOfMilkException OutOfSugarException LatteException
35
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 (); //... }
36
Exception Flow Out of milk Class Coffee Vending Machine Out of coffee Out of sugar getDrink() getCoffee() getLatte()
37
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
38
Motivation Reason #3 to use exceptions: Propagating Errors Up the Call Stack
39
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(); }
40
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
41
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!
42
Ex4
43
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.