1 / 24

EXCEPTIONS

EXCEPTIONS. What’s an exception??. Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen data error someone pulls a plug hardware error software error (blame it on the machine). Exceptions & Errors.

Download Presentation

EXCEPTIONS

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. EXCEPTIONS

  2. What’s an exception?? • Change the flow of control when something important happens • ideally - we catch errors at compile time • doesn’t happen • data error • someone pulls a plug • hardware error • software error (blame it on the machine)

  3. Exceptions & Errors • Exceptions are actual objects • They are instances of classes that inherit from the class Throwable • an instance of a Throwable class is created when an exception is thrown • Throwable has two sub classes • Error - internal runtime error (no hope) • Exception - we can do something here

  4. Error Class • These Exceptions are internal errors in the JAVA runtime environment (such as) • LinkageError • NoClassDefFoundError • NoSuchMethodError • OutOfMemoryError • VirtualMachineError

  5. Exception class • is divided into three major categories: • >Runtime exception classes • >I/O exception classes • >other classes that are directly derived from the Exception class

  6. RunTimeException • We know these guys (they are all classes) • NullPointerException • IllegalArgumentException • NumberFormatException • IllegalThreadStateException • IndexOutOfBoundsException • StringIndexOutOfBoundsException • Array IndexOutOfBoundsException

  7. I/O Exceptions • General I/O failures • EOFException • FileNOtFoundException • MalformedURLException • SocketException

  8. other classes of Exceptions • These exceptions are their own classes derived from the Exception class (I think) • ClassNotFoundException • AWTException • Instantiationexception • InterruptedException

  9. Some terms • exception - an error condition at run time • throwing - cause an exception to occur • catching - capturing an exception and executing some statements to try and resolve the error • catch clause - the block of code that does this (the catching) • stack trace - sequence of method calls that brought control to this point

  10. What causes an exception? • Two ways • explicitly - the programmer throws an exception with the throw statement • EX throw ExceptionObject • implicitly - carry out some illegal action • EX divide by zero • array index out of bounds • (we know that one)

  11. errors that might be thrown • We are using a JAVA provided method and get the following compiler error • blah blah: Exception blah blah must be caught or it must be declared in the Throws clause of this method • what’s going on???

  12. try/catch blocks • a JAVA method can indicate the kinds of errors it might throw • EX - a method that reads a file might throw an I/OException error • you have to protect your code against these errors. • This is compiler enforced • we use the try/catch blocks to do this

  13. The try part • You put a try block around the code that might throw an exception • the try statement says “try these statements and see if you get an exception • try { • ‘statements’ • }

  14. the catch part • Still have errors - missing the catch block • the catch block says “I will handle any exception that matches my argument” • a try block is followed by a catch block • catch(Exception e) • { • statements • }

  15. an example • Thread.sleep might throw an • InterruptedException • try { • Thread.sleep (500); • } • catch (InterruptedException e) • { // does nothing }

  16. Added notes • A try block may have many statements • therefore, the code in the try block may throw more than one exception • there may be more than one catch block following a try block • try{ } • catch(IOException e1) { } • catch(InterruptedException e2) { } • catch(SocketException e3) { }

  17. the finally clause • Assume there is some code you absolutely must do • put the code in a finally block • the finally block executes whether an exception was thrown or not, if an exception was caught or not, and will execute even if a return has been executed

  18. uses of the finally clause • You open a file • you read the file • you must close the file • (weather an exception was thrown or not) • return resources • dispose of a socket • dispose of a file descriptor

  19. create your own exceptions • The new class should inherit from some other exception in the JAVA hierarchy • inherit from Exception, not Error • you can inherit from Exception but try and find one ‘close’ the one you are creating • an Exception usually has two constructors • first takes no parameters • second takes a String as a parameter

  20. example - create an Exception • public class Oops extends Exception { • public Oops() {}; • public Oops (String msg) { • super(msg); • } • }

  21. example - create an Exception • Public class Oops extends IOException { • public Oops() {}; • public Oops (String msg) { • super(msg); • } • }

  22. new methods and exceptions • You can create a new method that throws an exception by using the throws keyword • modifiers return name() throws e1, e2, e3 • public void ReadF() throws Something { • Something s = new Something(“oops”); • if ((x /y) > 4) • throw s;

  23. handling the Exception • When an Exception occurs, execution stops • the machine looks for a catch clause to handle the error • if no catch clause is found in the offending method to handle the error, (final is run) and control is passed to the ‘calling’ method • this method is searched for a catch clause • and so on up the ‘call chain • you bomb if no catch clause is found

  24. some fancy stuff • Two methods are inherited from Throwable • Did you wonder why there was that String in the second Exception constructor example?? • There is a method that returns that String • can be used to pass information • the method getMessage returns that String • another method printStackTrace() will print the call chain from the point of the exception

More Related