Exceptions Flashcards
What are the 4 Policies for Handling Runtime Errors?
- Resumption
- Error rolling
- Long jumps
- Exceptions
What is the meaning of Resumption error handling policy?
On error, the offending command is retried.
Common methodology: invoke a “handler”; retry after the handler is done.
What is the meaning of Error rolling error handling policy?
Function indicates success or failure via its return value.
Caller should properly handle errors.
?How is Longjump policy implemented in C
The function setjmp() marks the current code location as “come here if something wrong happens”. (returns 0)
The function longjmp() jumps to the error handling code. (returns error code)
Pros and cons of resumption policy?
Pros:
1. The offending command is not aware of the problem nor of the solution (blissful programming)
Cons:
- Perplexing Situation: You need the context to deal with the error, but you don’t have it.
- Hardly Ever Works: In most cases, the handler can try, but cannot promise, to fix the problem.
Error rolling pros and cons
Pros:
- Full flexibility
- Can choose to ignore errors
Cons:
1. Heavy responsibility on the programmers
Long jumps Pros and Cons
Pros: 1. Error-flagging code does not clutter logic flow 2. No need for lots of nested ifs Cons: 3. Painfully unsafe
?What does catch or declare means in Java
every exception that can arise needs to be catched or added to the method type.
Why finally clause is useful in a try block?
- for managing allocated resources
2. because the handlers may throw exceptions themselves
What does RAII means?
Resource Acquisition Is Initialization.
Acquire on initialization, release on destruction.
What is the purpose of the ‘using’ keyword in C#?
RAII is inappropriate for garbage-collected languages.
Instead C# uses the keyword using:
using (StreamReader input = new StreamReader(“from.txt”))
from.txt will be closed on destruction of input.