90 likes | 99 Views
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.
E N D
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
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
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; }
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); } }
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
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(); }
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 } } }
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(); }
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