CPSC 2150 - DAY 22 NOVEMBER 29, 2016 ================================================================================ EXCEPTIONS ARE NOT APPROPRIATE WHEN: .If a bad situation can be specified in a precondition in a contract and can be checked, when necessary by clients, then exceptions are not suitable .Ex. stack pop operation should just include a precondition that stack is not empty .Raising an exception is inefficient. .See the decorator pattern discussion on precondition checking wrappers that can be added or removed. .If the bad situation is out of the control of the application .Hardware error .Resource avaliability error EXCEPTIONS: .An exception indicates a problem with an application that entails a dramatic change in control flow .Vocabulary: Exceptions and Errors are .thrown by a componenet implementation .caught by a client .In a client, a try-catch statement is used to catch exceptions try { statements } catch (exceptionType1 identifier1) { handler for type 1 } catch (exceptionType2 identifier2) { handler for type 2 } If something is thrown during execution of the statements in the try block 1. The rest of the code in the try block is skipped 2. The catch clause are examined top to bottom for the first matching catch. 3. If appropriate catch clause is found: .The body of the catch clause is executed. .The remaining catch clauses are skipped. 4. If no such catch clause is found: .The exception is thrown to the outer block, which is .A try block (that potentially handles it, in the same manner) .A method body (resulting in it being thrown to its client) FINALLY CLAUSE: .Some actions should be performed whether or not exceptions occur .Example: releasing resources such as database or network connections .Syntax: try { ... } catch (IOException e) { ... } finally { ... } WHEN TO USE EXCEPTIONS: Best practice suggests exceptions should be reserved for unexpected situations: .Problems external to the application .Resource exaustion .Recoverable problems that cannot be handled with checkable preconditions in contracts. .Ex. a file does not exist because it has been deleted after its existance has already been confirmed. UNCHECKED VS. CHECKED EXCEPTIONS: Unchecked exceptions are: .Error and its subclasses .RuntimeException and its subclasses. The rest are checked exceptions .Checked means that the compiler checks that a method whose body contains a statement that might throw the exception either catches it, or explicitly propogates it up the call chain by declaring that it also throws an exception. STATIC MEMORY ALLOCATION: Arrays are appropriate: .When you can reasonably predict the bounds in advance .Efficient, predictable, and constant time access to entries is critical .Overhead needs to be minimized. DYNAMIC ALLOCATION IS APPROPRIATE: .When your application needs flexibility with the number of entries .When initialization duration needs to be short. .Collection Terminology .Fixed size means the size of a collection is inflexible .Flexible size means that the size is changeable