Exceptions Please sit with your Tivoo Group Today

  • Slides: 18
Download presentation
Exceptions Please sit with your Tivoo Group

Exceptions Please sit with your Tivoo Group

Today • You will build your own exception handling code • You will know

Today • You will build your own exception handling code • You will know about some of the finer details of Java exceptions – general vs. specific exception types, finally clauses, Runtime. Exceptions

int my. Value = 0 try { my. Value = 1; function. That. Throws.

int my. Value = 0 try { my. Value = 1; function. That. Throws. Exception(my. Value) } catch (Specific. Kind. Of. Exception e) { my. Value = 3; } return my. Value; What does this function return, if function. That. Throws. Exception throws an exception? 1. 0 2. 1 3. If it throws Specific. Kind. Of. Exception 3, otherwise 1 4. If it throws Specific. Kind. Of. Exception 3, otherwise 0 5. If it throws Specific. Kind. Of. Exception 3, otherwise it does not return

Basic Operation of Exceptions try { throw new Exception. Class(“Some data”) } catch (Other.

Basic Operation of Exceptions try { throw new Exception. Class(“Some data”) } catch (Other. Exception. Class e) { //if Other. Exception. Class is the same as //Exception. Class (or one if it’s superclasses) //we go here. Otherwise the exception continues // to the next try…perhaps outside of this function deal. With. Exception(e. get. Data()); } An uncaught exception can easily end your program.

The Use of Exceptions Improves Your Code by Consolidating Error Handling int result 1

The Use of Exceptions Improves Your Code by Consolidating Error Handling int result 1 = get. Data(); if(result 1 != -1) { result 2 = process. Data(result 1); if(result 2 != -1) { display. Data(result 2); return; } else { System. err. println(“Error processing data”); return; } else { System. err. println(“Error getting data”); }

Do not do this try { call. Function(); } catch (Catastrophic. Exception e) {

Do not do this try { call. Function(); } catch (Catastrophic. Exception e) { e. print. Stack. Trace(); //just continuing on, maybe hiding this //error with later code, corrupting data //files } return;

Do not do this try { call. Function 1(); } catch (Exception e) {

Do not do this try { call. Function 1(); } catch (Exception e) { e. print. Stack. Trace(); } try { call. Function 2(); } catch (Exception e) { e. print. Stack. Trace(); }

The Use of Exceptions Improves Your Code by Consolidating Error Handling try { int

The Use of Exceptions Improves Your Code by Consolidating Error Handling try { int result 1 = get. Data(); result 2 = process. Data(result 1); display. Data(result 3); } catch (Processing. Exception e) { //let the functions themselves //decide what to display in their //error System. err. println(e); }

The Use of Exceptions Improves Your Code By Making it Easy to Move Up

The Use of Exceptions Improves Your Code By Making it Easy to Move Up the Call Hierarchy • In main, we request the user enter a file name. We take the name give and call process file. • In process file, we open the file and call do. XMLParse • In do. XMLParse we break the file into events. For each even we call parse. Event • In parse. Event we get the date string out of the event XML. Then we pass it to parse. Date • In parse. Date, we discover that date string is malformed. ERROR!

The Use of Exceptions Improves Your Code by Allowing You to Report Relevant Data

The Use of Exceptions Improves Your Code by Allowing You to Report Relevant Data in Your Exception • If there is a parsing error, I want to know – What line of the data file the error occurred on – What the problematic text was – If I was “expecting” something – what was I expecting • If there is an SQL error writing to the database, I want to know – The SQL I was trying to execute • If there was an network error – The URL I was trying to read from • DO NOT hesitate to write your own exception classes!

Fail or Retry? • Some things are unreliable: a network connection – maybe it

Fail or Retry? • Some things are unreliable: a network connection – maybe it makes sense try again in a half second (but don’t get caught in an infinite loop) • Sometimes you can ignore a problem in a small part and still get 95% of what you need done • Often a retry is possible but the point of failure is not the place for a retry – throw and let the proper place catch • DO NOT let your code just return bad data and move the problem to someplace that’s harder to debug. Fail. • DO NOT do a retry without logging the problem • DO NOT accept that your code will frequently print meaningless error messages or other garbage (TERRIBLE STYLE)

Activity • Modify your parsing classes to use exceptions appropriately • Make your own

Activity • Modify your parsing classes to use exceptions appropriately • Make your own exception classes – include every imaginable bit of useful error data (hint…you might have to add some tracking to allow detailed reporting) • For recoverable errors (e. g. problems with 1 entry out of many) log and recover

Today • You will build your own exception handling code • You will know

Today • You will build your own exception handling code • You will know about some of the finer details of Java exceptions – general vs. specific exception types, finally clauses, Runtime. Exceptions

Don’t Make Your Catch Clauses General try { parse 1(); parse 2() parse 3()

Don’t Make Your Catch Clauses General try { parse 1(); parse 2() parse 3() } catch (Exception e) { //this code is a lie System. err. println(“Error parsing”); }

If this is ordinary… public void parse. Event() throws Parse. Exception // callers are

If this is ordinary… public void parse. Event() throws Parse. Exception // callers are required to catch this, or throw // themselves

For More Unusual Circumstances if(db. is. Not. Connected()) { //This would be super bizarre

For More Unusual Circumstances if(db. is. Not. Connected()) { //This would be super bizarre throw new Runtime. Exception(“DB is not connected in function close. DB. What is up? ”); }

Finally try { file. open(); //seriously this must be closed do. ABunch. More. Stuff(file);

Finally try { file. open(); //seriously this must be closed do. ABunch. More. Stuff(file); } finally { // runs regardless of any exceptions that might // be thrown file. close(); } Try with Resources is a new feature in Java 7 that does a similar thing

Mikes Hints for Exceptions • Use exceptions to put error handling in the place

Mikes Hints for Exceptions • Use exceptions to put error handling in the place where it belongs. Often, that means putting it very close to the user. • Always document everything you can (easily) in an exception • Prefer throw to returning a bogus value, especially one like null that can cause problems elsewhere • Know about runtime exceptions when you need quick & dirty “the world has exploded” exceptions • Don’t be shy about throwing or defining your own exception types