{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec09_Exceptions_2per - EECS 285 EECS285 Lecture 09...

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

View Full Document Right Arrow Icon
1 EECS 285 EECS285 Lecture 09 Exceptions Van der Linden Ch. 10 EECS EECS 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
Image of page 1

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

View Full Document Right Arrow Icon
2 EECS 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 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
Image of page 2
3 EECS 285 5 Andrew M. Morgan Exceptions Example public static void main(String [] args) { int [] ary = new int[5]; try { out.println("A"); ary[7] = 15; out.println("B"); } catch (java.lang.ArrayIndexOutOfBoundsException excep) { out.println("C"); } //poor style finally { out.println("D"); } //...
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern