must use the parameter as its argument A no argument constructor must also be

Must use the parameter as its argument a no argument

This preview shows page 15 - 23 out of 28 pages.

must use the parameter as its argument A no-argument constructor must also be included whose body begins with a call to super This call to super must use a default string as its argument
Image of page 15
Multiple catch Blocks A try block can potentially throw any number of exception values, and they can be of differing types In any one execution of a try block, at most one exception can be thrown (since a throw statement ends the execution of the try block) However, different types of exception values can be thrown on different executions of the try block Different types of exceptions can be caught by placing more than one catch block after a try block Any number of catch blocks can be included, but they must be placed in the correct order
Image of page 16
Multiple catch Blocks
Image of page 17
Catch the More Specific Exception First When catching multiple exceptions, the order of the catch blocks is important When an exception is thrown in a try block, the catch blocks are examined in order The first one that matches the type of the exception thrown is the one that is executed catch (Exception e) { . . . } catch (NegativeNumberException e) { . . . } Because a NegativeNumberException is a type of Exception , all NegativeNumberExceptions will be caught by the first catch block before ever reaching the second block The catch block for NegativeNumberException will never be used! For the correct ordering, simply reverse the two blocks
Image of page 18
Throwing an Exception in a Method Sometimes it makes sense to throw an exception in a method, but not catch it in the same method Some programs that use a method should just end if an exception is thrown, and other programs should do something else In such cases, the program using the method should enclose the method invocation in a try block, and catch the exception in a catch block that follows In this case, the method itself would not include try and catch blocks However, it would have to include a throws clause
Image of page 19
Throwing an Exception in a Method
Image of page 20
Declaring Exceptions in a throws Clause If a method can throw more than one type of exception, then separate the exception types by commas public void aMethod() throws AnException, AnotherException If a method throws an exception and does not catch it, then the method invocation ends immediately
Image of page 21
throws Clause in Derived Classes A method with throws clause defined in a superclass can be overridden in a subclass When such a method is overridden, you: Can't add any exceptions to the throws clause of the overriding method
Image of page 22
Image of page 23

You've reached the end of your free preview.

Want to read all 28 pages?

  • Winter '12
  • Ghouti
  • Subroutine, throw-catch Mechanism

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture