Day-11-Exceptions - CSE116 CSE504 Introduction to Computer...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE116 / CSE504 Introduction to Computer Science II Dr. Carl Alphonce 343 Davis Hall [email protected] Office hours: Tuesdays 9:00 AM – 12:00 PM And also by appointment (request appointment via e-mail) ELECTRONICS OFF AND AWAY
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(partof)Iteratorinterface /** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #next} would * return an element rather than throwing an exception.) * * @return {@code true} if the iteration has more elements */ boolean hasNext(); /** * Returns the next element in the iteration. * * @return the next element in the iteration */ E next();
Image of page 2
next()method What should next() method do when hasNext() returns false? Have a discussion with your neighbors!
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Whatareexceptions? Exceptions are a mechanism for handling “exceptional” situations which can occur at runtime. Many languages provide exceptions. Terminology: code where something unexpected happens “throws” an exception code which handles the exceptional situation “catches” the exception – this code is called an exception handler
Image of page 4
Whenareexceptionsappropriate? Exceptions are appropriate to use to signal to a caller a problematic situation which cannot be handled locally. Example: Consider a file writing component which is used both by an interactive UI and a batch processing component. If the file writing component has a problem (e.g. “disk is full”), can it decide locally how to respond? No. It is up to the client to decide how to react. The UI may notify its human user. The batch processor may log the problem, and skip processing of this file.
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Whenareexceptions not appropriate? It is not appropriate to use the exception mechanism when an exceptional situation can be handled locally. It is not appropriate to use the exception mechanism in dealing with situations which are not exceptional. If a particular situation is expected, it should be explicitly checked for. For example, if a user supplies the name of a file to be read, it is better to check for existence of the file rather than to attempt to read and rely on a thrown exception to give notice if the file doesn’t exist.
Image of page 6
Howareexceptionsgenerated? An exception is an object An exception class must derive from the java.lang.Exception class Detour into inheritance and typing…
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Typesandsubtypes Every class/interface in Java defines a type.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern