1 / 9

Effective Exception Handling in Object-Oriented Programming

Learn about error handling options, exception programming, throwing and catching exceptions, useful hints, and creating custom exception classes. Know when and how to handle errors gracefully.

marieb
Download Presentation

Effective Exception Handling in Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming DesignTopic : Exception Programming Maj Joel Young Joel.Young@afit.edu Maj Joel Young

  2. Error Handling • General error handling options • Notify the user, and • Return to a safe state and resume program, or • Terminate program gracefully • Exception handling • Transfer control from where error occurs to an error handler that can handle the situation • Alternate execution path • Types of errors • User input errors – usually handled in GUI • Device errors – handle with exception • Physical limitations – handle with exception • Code errors – best to test and eliminate

  3. Java Exceptions • Throwing exceptions • Find or create an appropriate exception class • Advertise that method can throw an exception • Make an instance of exception class • Throw the exception String readData(BufferedReader in) throws EOFException { ... while(...) { if (ch == -1) // EOF Encountered { if (n < len) throw new EOFException(); } ... } return s; }

  4. Creating Exception Classes • If available exception classes do not handle your problem…then, create your own exception class FileFormatException extends IOException { public FileFormatException() {} public FileFormatException(String gripe) { super(gripe); } }

  5. Catching Exceptions • try/catch block try { code more code ... } catch (ExceptionType e) { handler for this type } • If any code in try block throws an exception of the specified type, then • The program skips the remainder of the code in the try block • The program executes the handler code inside the catch clause • The method exits • Otherwise, if none of the code in try block throws an exception, the method skips the catch clause • If the code throws an exception of a different type, the method exists immediately

  6. Catching Exceptions public void read(BufferedReader reader) { try { boolean done = false; while (!done) { String line = reader.readLine(); if (line == null) // end of file done = true; else { process line } } } catch (IOException exception) { exception.printStackTrace(); }

  7. Passing Exceptions • If your methods is not equipped to handle a method, it can be passed up to the caller public void read(BufferedReader reader) throws IOException { boolean done = false; while (!done) { String line = reader.readLine(); if (line == null) // end of file done = true; else { process line } } }

  8. More on exceptions • Multiple exceptions • Rethrowing exceptions • The finally clause Graphics g = image.getGraphics(); try { code that might throw exceptions } catch (MalformedURLException e1) { // action for handling malformed URLs throw e1; // rethrow exception – pass to caller } catch (IOException e2) { // action for handling IO exceptions } finally { g.dispose(); }

  9. Hints for using exceptions • Exception handling costs time, so use only where appropriate • Hints • Exception handling is not supposed to replace a simple test • E.g. check if stack is empty before popping rather than throwing an exception when trying to pop and empty stack • Do not micromanage exceptions • No need to put every statement in try block • Do not squelch exceptions • As a minimum print some sort of error message • Propagating exceptions is not a sign of shame • Sometimes calling methods are better equipped to handle the exception

More Related