DOC PREVIEW
Berkeley COMPSCI 61B - Lecture Notes

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61B Lecture #9Parent constructorsWhat to do About Errors?Catching ExceptionsExceptions: Checked vs. UncheckedUnchecked ExceptionsChecked ExceptionsCS61B Lecture #9Notice: From now on, theBlue Rea derwill refer to the r eader thatsays “Assorted Materials on Java” on the first pag e inside the frontcover. Likewise, theYellow Readerwill refer to the reader that says“Data Structures (Into Java)” inside its front cover. The las t readerwill just be theJava Reference Manual.Clarification: For lecture #5. In view of some confusion in the lastlecture, I have added some stuff to slide 14 (‘Instance’ and ‘Static’Don’t Mix).Today: Various odds and ends in support of abstraction.Project 1 handed out today. Skeleton files will be ready Real Soon,but there’s lots to do without them.Last modified: Mon Sep 20 11:42 :27 2004 CS61B: Lecture #9 1Parent constructors• In lecture notes #5, talked about how Java allows implementer of aclass to control all manipul ation of objects of tha t class.• In particular , this means that Java gives the cons tructor of a classthe first shot at each new object.• When one cla ss ex te nds another , there are two cons tructors—onefor the parent type and one for th e n ew (child) type.• In this case, Java guar antees that one of the parent’s constructorsis ca l l ed fir st. In effect, there is a call to a parent constructor atthe beginning of every one of the child’s constructors.• You can call the parent’s constructor yourself. By default, Java callsthe “default” (parameterless) constructor.class Figure { class Rectangle extends Figure {public Figure (int side s ) { public Rectangl e () {... super (4);}... }...} }Last modified: Mon Sep 20 11:42 :27 2004 CS61B: Lecture #9 2What to do About Errors?• Large amount of any production program devoted to dete cting andresponding to error s.• Some errors ar e external (bad input, network fa i l ures); others areinterna l e rrors in programs.• When method has stated precondi ti on, it’s the client’s job to comply.• Still, it’s nice to detect and report client’s errors.• In Java, wethrow exce ption objects,typically:throw newSomeException(optional descrip ti on);• Exceptions are objects. By convention, they ar e given two construc-tors: one with no arguments, and one with a descr i ptive string argu-ment (which the exception stores).• Java system throws some e xceptions imp l i ci tly, as w hen you deref-erence a null pointer, or excee d an array boun d.Last modified: Mon Sep 20 11:42 :27 2004 CS61B: Lecture #9 3Catching Exceptions• A throw causes each active method call toterminate abruptly,until(and unless) we come to a try block.• Catch excep ti ons and do something corrective with try:try {Stuff that mig ht throw exception;} catch (SomeExceptione) {Do something r easonable;} catch (SomeOtherExce ptione) {Do something el se reasonable;}Go on with life;• WhenSomeExceptionexception occurs in “Stuff. . . ,” we immedi-ately “do something reas onable” and then “go on with life . ”• Descriptive stri ng (if any) available as e.getMess age() for err ormessages and the like.Last modified: Mon Sep 20 11:42 :27 2004 CS61B: Lecture #9 4Exceptions: Checked vs . Unchecked• The object thrown by throw command mus t be a subtype of Throwable(in java.lang).• Java pre -decl ares several such subtypes, among them– Error, used for serious , unrecoverable errors;– Exception, intended for all oth er exception s;– RuntimeException, a subtype of Exc eption intended mostly forprogrammin g errors too common to be wor th decl a ring.• Pre-declare d excepti ons are all subtypes of one of the se.• Any subtype of Erro r or Run timeExceptio n is said to beunchecked.• All other exception types arechecked.Last modified: Mon Sep 20 11:42 :27 2004 CS61B: Lecture #9 5Unchecked Exceptions• Intended for– Programmer errors: many library functions throwIllegalArgumentEx cep tion when one fails to meet a pr econdi-tion.– Errors detected by the basic Java system: e.g.,∗ Executing x.y when x is null,∗ Executing A[i] when i is out of bounds,∗ Executing ( String) x wh en x turns out not to poi nt to a String.– Certain ca ta strophic failures, such as running out of memory.• May be thrown anywhere at any time with no s pecial p reparati on.Last modified: Mon Sep 20 11:42 :27 2004 CS61B: Lecture #9 6Checked Exceptions• Intended to indicate exceptional cir cums ta nces tha t are not neces-sarily progr a mme r errors. Ex amples:– Atte mpting to open a file that does not exi st.– Inp ut or output errors on a file.– Re ceiving an interrup t.• Every checked ex ception tha t can occur inside a method must ei-ther be handled by a try statement, or reported in the method’sdeclaration.• For examp l e,void myRead () th r o ws IOExcept i on, InterruptedE x c e ption { ... }means that myRead (or something it calls)mightthrow IOExceptionor Interr uptedException.• Overriding methods may not declare additional checked exceptions .[Why not?]Last modified: Mon Sep 20 11:42 :27 2004 CS61B: Lecture #9


View Full Document

Berkeley COMPSCI 61B - Lecture Notes

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?