Download presentation
Presentation is loading. Please wait.
Published byEmerald Jennifer Shepherd Modified over 9 years ago
1
and other languages…
2
must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller Function label to subprogram Fortran strategy
3
An exception is an unusual event detectable by either hardware or software An exception is raised/thrown when its associated event occurs The exception handler code determines what to do
4
Error detection code is tedious to write and it clutters the program Exception propagation allows a high level of reuse of exception handling code Encourage programmers to consider all events that could occur that might need to be handled – especially checked exceptions.
5
http://google- styleguide.googlecode.com/svn/trunk/cppguide.html#Exceptions http://www.parashift.com/c++-faq/exceptions-avoid-spreading-out- error-logic.html http://www.parashift.com/c++-faq/exceptions-avoid-spreading-out- error-logic.html http://stackoverflow.com/questions/1744070/why-should- exceptions-be-used-conservatively http://stackoverflow.com/questions/1744070/why-should- exceptions-be-used-conservatively http://www.joelonsoftware.com/items/2003/10/13.html http://www.joelonsoftware.com/items/2003/10/13.html And a review: https://files.ifi.uzh.ch/rerg/arvo/courses/kvse/uebungen/Dijkstra_ Goto.pdf Put some thoughts on paper, be ready to share
6
How to bind an exception to a handler What information to make available about exception Is it possible to continue after an exception? Does the language have any built-in exceptions? Are there user-defined exception types Is there a default exception handler? Can exceptions be disabled? Class participation: What decisions were made for Java?
7
Added to C++ in 1990 Exceptions are user- or library-defined (none in language definition) Exception Handlers Form (like Java): try { -- code that is expected to raise an exception } catch (formal parameter) { // only 1 parm -- handler code }... catch (formal parameter) { -- handler code }
8
catch is the name of all handlers--it is an overloaded name, so the formal parameter of each must be unique The formal parameter need not have a variable ◦ It can be simply a type name to distinguish the handler it is in from others, e.g. catch(int) rather than catch (int x) The formal parameter variable can be used to transfer information to the handler The formal parameter can be an ellipsis (…), in which case it handles all exceptions not yet handled Compare to Java: Possible to throw/catch an int? Does formal parameter need a variable? (yes) How does the handler get information? What mechanism is used in place of …? How does this work?
9
Exceptions all raised explicitly by: throw expression; expression may be a literal or variable of any type A throw without an operand can only appear in a handler; when it appears, it simply re-raises the exception, which is then handled elsewhere Syntax to throw exception in Java?
10
Problem! main calls function a a calls function b exception occurs in b If no try/catch in b, exception propagates to a If no try/catch in a, exception propagates to main If no try/catch in main, execution terminates (in C++, can override default handler, named unexpected) Continuation. If exception is handled, execution continues after last catch (unless handler calls exit of course). main a b call Both Java and C++ propagate exceptions
11
The Java library includes two subclasses of Throwable : – Error Thrown by the Java interpreter for events such as heap overflow Never handled by user programs – Exception User-defined exceptions are usually subclasses of this Has two predefined subclasses, IOException and RuntimeException (e.g., ArrayIndexOutOfBoundsException and NullPointerException)
12
Exceptions of class Error and RunTimeException and all of their descendants are called unchecked exceptions All other exceptions are called checked exceptions (compiler checks that your code handles – all exceptions occur at runtime) Methods containing code that can throw a checked exception must either: ◦ List the exception(s) in a throws clause, or ◦ Handle the exception (e.g., with try/catch) ◦ Example throws clause (not the same as throwing an exception!): public void myFunction() throws Exception
13
“Checked” means that Java checks that you know a line of code can generate an error The compiler does NOT know that an exception will occur… it knows that an exception might occur! ALL exceptions occur at RUNTIME
14
A method that calls a method with a checked exception in its throws clause has three alternatives: 1. Catch and handle the exception 2. Catch the exception and throw an exception that is listed in its own throws clause 3. Declare that exception in its throws clause and do not handle it public void A() throws BadException { … } public void B() { … try { A() } catch (BadException e) { … } } public void C() throws AnException{ … try { A() } catch (BadException e) { throw (AnException) } } public void D() throws BadException { … A() … }
15
Point p; Point p2 = new Point(3,4); try { int x = p.getX(); } catch (NullPointerException e) { syso(“there’s a logic but here”); } // compiler doesn’t execute… so even though // we know p2 is OK, the compiler wouldn’t try { int x = p2.getX(); } catch (NullPointerException e) { syso(“there’s a logic but here”); }
16
A method cannot declare more exceptions in its throws clause than the method it overrides. Example: If you override run in a Thread class, you cannot add a throws clause.
17
Can appear at the end of a try construct Form: finally {... } Purpose: To specify code that is to be executed, regardless of what happens in the try construct (e.g., to close file or database connection). Finally is executed even if there is a return statement in the block. http://stackoverflow.com/questions/161177/does-c-support-finally-blocks-and-whats-this-raii-i-keep-hearing-about
18
try { for (index = 0; index < 100; index++) { … if (…) { return; } //** end of if } //** end of try clause finally { … } //** end of try construct * Java exercise has examples to play with
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.