Download presentation
Presentation is loading. Please wait.
Published byCaitlin O’Brien’ Modified over 8 years ago
1
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during program execution typically a large portion of code checks for error conditions we need special handling – to abort program right away – to avoid deeply nested if statements
2
Exceptions Usage used in a program in two ways: callee throw s an exception when exception has occurred caller catch es an exception, to execute code that handles the error/unusual event exceptions are objects – their type decides what to do – there is large class hierarchy of exceptions several exceptions may occur in one block of code
3
Examples of Exceptions a method call on variable whose value is null – (does not reference an object) – throws NullPointerException access an array element that does not exists – throws ArrayIndexOutOfBoundsException access a character in a String that does not exists – throws StringIndexOutOfBoundsException division by zero – throws ArithmeticException parameter of parseInt() method is a non-integer value – throws NumberFormatException – int n = Integer.parseInt("five"); //should be an integer such as "5"
4
Processing of Exceptions there are two types of exceptions – Exception s that you have to catch – Exception s that you don't need to catch but you can catch them when catching an exception, you have several choices do nothing – program ends when the exception occurs – a bad programming practice! catch and handle the exception completely catch the exception in another method – in a method that called your method – exception propagation (pass the buck) catch and handle the exception partially and throw an exception so that the caller(s) can catch and handle it, too – throw the same exception – throw another exception of the same type – throw another exception of completely different type (your own?)
5
Processing of Exceptions do nothing = bad programming program stops and the console shows an error message e.g. as being off by 1 when accessing an array we do not want our program to “crash” use try - catch blocks to account for possible errors especially user input is error-prone and errors must be handled gracefully
6
try block try block of code contains statements which may throw an exception – try { statement1; statement2; statementN; } … statements within the try block execute only while no exceptions are throw n In other words, if an exception is throw n, the remaining code will not execute
7
catch block catch block of code contains statements which handle an exception of a particular type – catch ( type_of_Exception variable) { statement1; statement2; statementN; } … code within the catch block executes only if an exception is thrown which has same type as the type type_of_Exception the variable is an object that can be further used in code
8
Syntax One try block is immediately followed by 1 or more catch blocks – try { statement(s); } catch (Type1_of_Exception variable){ statements; } catch (TypeN_of_Exception variable){ statements; } if no exception is thrown – control continues after the last catch block if an exception is thrown in the try block, – control skips over the remaining statements in the try block, – and continues in the catch block which corresponds to the exception’s type – once control comes to end of that catch block – control continues to the statement that follows the last catch block If there is no match, the program “crashes”
9
Propagation of Exceptions Suppose that methodThrowingSomeException() throws SomeException A method that calls methodThrowingSomeException() – can either try to call it and catch SomeException : – void eagerMethod () throws SomeException { try {methodThrowingSomeException(); } catch (SomeException error) {…} } – or propagate this SomeException to its caller then it must declare that it throws SomeException : – void lazyMethod () throws SomeException { methodThrowingSomeException(); } – or propagate this SomeException (or another) to its caller – void goodMethod () throws SomeException { try {methodThrowingSomeException(); } catch (SomeException error) { … throw error; // or throw new MyException(); } }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.