30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

474 Exception Handling Chapter 11<br />

d) A Finally block is optional after a Try block that does not have any corresponding<br />

Catch handlers.<br />

e) If a Finally block appears in a method, that Finally block is guaranteed <strong>to</strong> execute.<br />

f) It is possible <strong>to</strong> return <strong>to</strong> the throw point of an exception using keyword Return.<br />

g) Exceptions can be rethrown.<br />

h) A checked context causes a syntax error when integral arithmetic overflow occurs.<br />

i) Property Message returns a String indicating the method from which the exception<br />

was thrown.<br />

j) Exceptions can be thrown only by methods explicitly called in a Try block.<br />

ANSWERS TO SELF-REVIEW EXERCISES<br />

11.1 a) synchronous, asynchronous. b) throw. c) Finally. d) Exception. e) throw point.<br />

f) termination. g) unwind. h) FormatException. i) SystemException. j) checked.<br />

11.2 a) False. Exceptions can be handled by other methods on the method-call stack. b) False. <strong>Program</strong>mer-defined<br />

exception classes should extend class ApplicationException. c) True. d)<br />

False. A Try block that does not contain any Catch handler requires a Finally block. e) False.<br />

The Finally block executes only if program control enters the corresponding Try block. f) False.<br />

Return causes control <strong>to</strong> return <strong>to</strong> the caller. g) True. h) False. A checked context causes an OverflowException<br />

when arithmetic overflow occurs at execution time. i) False. Property Message<br />

returns a String representing the error message. j) False. Exceptions can be thrown by any method,<br />

regardless of whether it is called from a Try block. The CLR also can throw exceptions.<br />

EXERCISES<br />

11.3 Use inheritance <strong>to</strong> create an exception base class and various exception-derived classes. Write<br />

a program <strong>to</strong> demonstrate that the Catch specifying the base class catches derived-class exceptions.<br />

11.4 Write a program that demonstrates how various exceptions are caught with<br />

Catch exceptionParameter As Exception<br />

11.5 Write a program demonstrating the importance of the order of exception handlers. Write two<br />

programs, one with correct ordering of Catch handlers (i.e., place the base-class exception handler<br />

after all derived-class exception handlers) and another with improper ordering (i.e., place the baseclass<br />

exception handler before the derived-class exception handlers). Show that derived-class exceptions<br />

are not invoked when Catch handlers are ordered improperly.<br />

11.6 Exceptions can be used <strong>to</strong> indicate problems that occur when an object is being constructed.<br />

Write a program that shows a construc<strong>to</strong>r passing information about construc<strong>to</strong>r failure <strong>to</strong> an exception<br />

handler. The exception thrown also should contain the arguments sent <strong>to</strong> the construc<strong>to</strong>r.<br />

11.7 Write a program that demonstrates rethrowing an exception.<br />

11.8 Write a program demonstrating that a method with its own Try block does not have <strong>to</strong><br />

Catch every possible exception that occurs within the Try block. Some exceptions can slip through<br />

<strong>to</strong>, and be handled in, other scopes.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!