Exceptions In Java the designers of the language looked at a number of languages, and picked the best features from these language to put into Java, and the worst feat rues they attempted to fix in Java one of the fix is all about error handling In COBOL when I open a file I get back a 92 code, which means something failed In COBOL when I call code, I get back a "Status Value" and I should check it. But I do not always check these return values/status flags/etc So in Java we DO NOT have status flag, nor return values we have Exceptions, and you MUST handle them or your code will die ----------------------------------------------------- In Java I have: A) code running (my main) that will call a method of the Action class (my DAO) B) the Action (DAO) method start running, and connects to the DB 1) the connection works (no problem, I like this) 2) the connection fails (OOPS!!) this DAO method cannot fix the problem, the DB is off-line... so it need to tell the main (the code that call it) there was a problem the DAO method throws an Exception Exceptions are expensive to create and throw WARNING: when I read a file (not DB work) and I read record after record, at some point I will get to end-of-file this is expected. So an Exception is not the correct response, at end-of-file the Reader will return null --------------------------------------------------------------- in Java I code Exception handing in 2 parts: A) the code calling the method (my main in example above) B) the method being called (my DAO in the example above) Part A) I am calling a method that may or may not work so I code try/catch block the try block is what I want the code to do, the catch block ONLY runs if I catch and Exception If I do not want to code a try/catch block I can tell Java I AM NOT CATCHING the Exception, by adding a throws clause to my method so: RULE: I must catch or tell Java I am throwing the Exception Part B) in the method throwing an Exception, if the exception is a Runtime Exception, I do not have to list the exception on my method, if the exception is non-runtime I MUST list that exception on my method ====================================================== In Java: Throwable (an abstract class) | ------------------- | | Error Exception | ------------------------- | | RuntimeException RuntimeException do not require a try/catch block all other Exception do require a try/catch block (or throw them) RuntimeExceptions are call UNCHECKED because I do not have to code try/catch if my method throws non-runtime exception I MUST list that exception on my method, all non-runtime Exceptions are call CHECKED, because you have to code a try/catch ---------------------------------------------- If I opened a file or connected to a DB, I must close it, Java will not close it for me!!! if I code try/catch and my code "handles" the problem what about the open file/connection I can code a finally block (after the catch block) the finally block will ALWAYS run, every if I fail to handle it in this finally block I code the close/disconnect)