240 likes | 264 Views
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.
E N D
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 • 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
Error Class • These Exceptions are internal errors in the JAVA runtime environment (such as) • LinkageError • NoClassDefFoundError • NoSuchMethodError • OutOfMemoryError • VirtualMachineError
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
RunTimeException • We know these guys (they are all classes) • NullPointerException • IllegalArgumentException • NumberFormatException • IllegalThreadStateException • IndexOutOfBoundsException • StringIndexOutOfBoundsException • Array IndexOutOfBoundsException
I/O Exceptions • General I/O failures • EOFException • FileNOtFoundException • MalformedURLException • SocketException
other classes of Exceptions • These exceptions are their own classes derived from the Exception class (I think) • ClassNotFoundException • AWTException • Instantiationexception • InterruptedException
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
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)
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???
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
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’ • }
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 • }
an example • Thread.sleep might throw an • InterruptedException • try { • Thread.sleep (500); • } • catch (InterruptedException e) • { // does nothing }
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) { }
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
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
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
example - create an Exception • public class Oops extends Exception { • public Oops() {}; • public Oops (String msg) { • super(msg); • } • }
example - create an Exception • Public class Oops extends IOException { • public Oops() {}; • public Oops (String msg) { • super(msg); • } • }
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;
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
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