CSC 243 Java Programming Spring 2014 March 2014

  • Slides: 8
Download presentation
CSC 243 – Java Programming, Spring, 2014 March 2014 Week 6 ish, Exceptions

CSC 243 – Java Programming, Spring, 2014 March 2014 Week 6 ish, Exceptions

Exception constructs already in use • try { • attempt to run a block

Exception constructs already in use • try { • attempt to run a block of code • } catch (Type_A_Exception taex) { • Do something with taex, e. g. , taex. get. Message() • } catch (Type_A_Exception tbex) { • Do something with tbex, e. g. , print, then throw tbex • } finally { • Do this after all of the above, even after a return! • }

Unchecked and checked exceptions • javac forces client code to catch checked exceptions •

Unchecked and checked exceptions • javac forces client code to catch checked exceptions • unchecked exceptions need not be caught; they can be – java. lang. Error • java. io. IOError – maybe a disk drive goes off line – java. lang. Runtime. Exception • java. lang. Null. Pointer. Exception • Other java. lang. Exceptions are checked

Throwing a new exception • An explicit throw creates an Exception object. • public

Throwing a new exception • An explicit throw creates an Exception object. • public String get. Player. Name(int player. Index) throws Perquacky. Exception { • if (player. Index < 0 || player. Index >= get. Number. Of. Players()) { • throw new Perquacky. Exception("Error, get. Player. Name player index " • + player. Index + " is out of range 0 through " • + (get. Number. Of. Players()-1)); • } • return players[player. Index]; • }

Rethrowing an exception • A rethrow catches and handles an Exception object, then throws

Rethrowing an exception • A rethrow catches and handles an Exception object, then throws it again. • } catch (Number. Format. Exception nx) { • System. err. println(“Exception: “ • + nx. get. Message(); • nx. print. Stack. Trace(); // to System. err • throw nx ; // This is the rethrow

An implicit throw • An implicit throw allows a called method to throw an

An implicit throw • An implicit throw allows a called method to throw an Exception via the calling method. – public String [][] move(String command) throws Scrabble. Exception • Invokes “int used = players[next. Player]. delete. Tiles(validword); • But move() does not catch delete. Tiles’s exception: – public int delete. Tiles(String tileset) throws Scrabble. Exception • The remove() implicitly throws delete. Tile’s exception.

A Chained Exception • A chained Exceptions tacks a detail message onto an underlying

A Chained Exception • A chained Exceptions tacks a detail message onto an underlying cause Exception. – } catch (Number. Format. Exception nx) { – throw new Exception(“detail message”, nx); • Used to prepend a context-specific message to an Exception thrown from a called method. • The new Exception must have the appropriate constructor. It may be a custom Exception.

Custom Exceptions • A custom Exception inherits, directly or indirectly, from java. lang. Exception.

Custom Exceptions • A custom Exception inherits, directly or indirectly, from java. lang. Exception. – public class Scrabble. Exception extends Exception • public Scrabble. Exception(String text) – super(text); // Call to base class constructor. • Client code can explicitly catch a custom Exception.