Lec09_Exceptions_2per

Lec09_Exceptions_2per - 1 EECS EECS EECS EECS EECS EECS...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 EECS285 Lecture 09 Exceptions Van der Linden Ch. 10 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 2 Andrew M. Morgan Exceptions Overview Exceptions are basically a way of handling errors that come up Not always specific to errors, but most often Are exceptions necessary? No could use other techniques, such as boolean return values, lots of if statements, etc Should exceptions take the place of other error handling methods? No sometimes its just easier to check a return value Main benefit of exceptions Allows you to keep the (usually not executed) error handling code out of the main flow of your program Programs that don't use exceptions are often cluttered with "if (success)", or lots of checks for conditions that are most often NOT going to occur 2 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 3 Andrew M. Morgan Using Return Values For Error Notification public static void main(String args) { MyInt myRef = new MyInt(0); int divVal; setVal(16, myRef); divVal = 100 / myRef.getVal(); out.println("DivVal: " + divVal); } } Exception in thread "main" java.lang.ArithmeticException: / by zero setVal does some error checking, and returns a boolean indicating whether the function was successful or not But main ignores the return value (perfectly legal), and still results in a divide by zero Error occurs here though, after the error condition went unnoticed public class ReturnValDemo { static boolean setVal(int inVal, MyInt intRef) { boolean success = true; if (inVal > 100) { intRef.setVal(inVal); } else { success = false; } return (success); } public class MyInt { private int val; MyInt(int inVal) { val = inVal; } int getVal() { return (val); } void setVal(int inVal) { val = inVal; } } EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 4 Andrew M. Morgan Exception Terminology When a function comes across an exceptional situation, it should throw an exception An exception that was thrown should then be handled in a specified way, so that the program can recover, or exit gracefully When calling a function that throws an exception, the function may complete successfully, or it may fail Therefore, when you call the function, you are going to try it and see what happens. Within a try block, an exception might get thrown In order to correctly handle that exception, we must make an attempt to catch the exception that was thrown A catch block (or blocks) should come after a try block to handle the exception finally , whether or not an exception was thrown, there may be some stuff you want to do Statements in a finally block will always be executed, regardless of whether an exception was thrown or not 3 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 5 Andrew M. Morgan Exceptions Example public static void main(String args)...
View Full Document

This note was uploaded on 01/19/2012 for the course EECS 285 taught by Professor Idk during the Fall '08 term at University of Michigan.

Page1 / 9

Lec09_Exceptions_2per - 1 EECS EECS EECS EECS EECS EECS...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online