Chapter 10: Exceptions and I/O StreamsExceptions and I/O StreamsException HandlingSlide 4Slide 5Slide 6The try-catch StatementSlide 8The finally ClauseException PropagationSlide 11Slide 12Slide 13The Exception Class HierarchyThe throw StatementSlide 16Slide 17Checked ExceptionsUnchecked ExceptionsSlide 20The IOException ClassSlide 22Text FilesI/O Streams and I/O ExceptionsI/O StreamsI/O Streams - SubdivisionsCharacter vs. Byte StreamsData vs. Processing Streams11Chapter 10: Exceptions and I/O Chapter 10: Exceptions and I/O Streams Streams Original Slides by John Lewis and William LoftusOriginal Slides by John Lewis and William LoftusModified significantly byModified significantly byBob Roggio, July 2007Bob Roggio, July 200722/31/31 Exceptions and I/O StreamsExceptions and I/O StreamsNow we can explore two related topics further: exceptions and Now we can explore two related topics further: exceptions and input/output streamsinput/output streamsChapter 8 focuses on:Chapter 8 focuses on:Exception HandlingException HandlingUncaught ExceptionsUncaught ExceptionsThe try-catch statementThe try-catch statementThe finally ClauseThe finally ClauseException propagationException propagationThe Exception Class HierarchyThe Exception Class HierarchyChecked and unchecked ExceptionsChecked and unchecked ExceptionsI/O ExceptionsI/O ExceptionsI/O StreamsI/O Streams33/31/31 Exception HandlingException HandlingAn An exceptionexception is an is an objectobject that describes an unusual or erroneous that describes an unusual or erroneous situationsituationExceptionsExceptions are are thrownthrown by a program by a program oror the runtime environment, and the runtime environment, and may be may be caughtcaught and and handledhandled by another part of the program by another part of the programThis means that when an exception occurs, you can sometimes code This means that when an exception occurs, you can sometimes code what you want to happen.what you want to happen.An exception is an An exception is an objectobject that defines unusual or erroneous situations. that defines unusual or erroneous situations.An exception is something that should not normally happen or happen An exception is something that should not normally happen or happen infrequently.infrequently.Your book has a list of common exceptions to be thrown.Your book has a list of common exceptions to be thrown.Array index out of bounds; specified file cannot be found; division by zero, …Array index out of bounds; specified file cannot be found; division by zero, …44/31/31 Exception HandlingException Handling As its name implies, exceptions are ‘exceptions’ to the As its name implies, exceptions are ‘exceptions’ to the normal way of doing business and we can design efficient normal way of doing business and we can design efficient ways to handle them if/when they occur. ways to handle them if/when they occur. Program can deal with an exception in one of three ways:Program can deal with an exception in one of three ways:ignore itignore ithandle it where it occurshandle it where it occurshandle it an another place in the programhandle it an another place in the programThe manner in which an exception is processed is an The manner in which an exception is processed is an important design considerationimportant design consideration55/31/31 Exception HandlingException HandlingIf an exception is If an exception is ignoredignored by the program, the program will by the program, the program will terminateterminate abnormally and produce an appropriate message abnormally and produce an appropriate messageThe message includes a The message includes a call stack tracecall stack trace that indicates the line that indicates the line on which the exception occurredon which the exception occurredYou have no doubt seen these!You have no doubt seen these!The The call stack tracecall stack trace also shows the also shows the method call trailmethod call trail that lead to that lead to the attempted execution of the offending linethe attempted execution of the offending lineThe The getMessagegetMessage method returns a string explaining why the method returns a string explaining why the exception was thrownexception was thrownThe The printStackTraceprintStackTrace method prints the call stack trace method prints the call stack traceWe will see these ahead.We will see these ahead.66/31/31 // Zero.java Author: Lewis/Loftuspublic class Zero{ // Deliberately divides by zero to produce an exception. public static void main (String[] args) { int numerator = 10; int denominator = 0; System.out.println (numerator / denominator); System.out.println ("This text will not be printed."); }// end main()}// end ZeroOutput:Exception in thread “main” java.lang.ArithmeticException: / by zero at Zero.main (Zero.java:17) Note: tells problem and class.method where it occurred and statement number!There’s no code to handle this exception explicitly. The second System.out.println does not execute, because the exception occurs first.First line says what exception was thrown and provides some info about why it was thrown.Remaining lines are the call stack traceCan call methods in exception class: getMessage returns a string explaining reason for exception printStackTrace prints the call stack trace.Example: Uncaught Exception Call stack traceproblemException classFirst line says that Exception was thrown. Other lines: Call trace (ahead)Method, file, and line number where exception occurred.77/31/31 The try-catch StatementThe try-catch StatementTo process an exception when it occurs, the line that can cause To process an exception when it occurs, the line that can cause (throw) an exception is executed within a (throw) an exception is executed within a try blocktry blockA try block is followed by A try block is followed by one or moreone or more catchcatch clauses, which contain clauses, which contain code to process an exceptioncode to process an exceptionThe catch clauses are called exception handlers.The catch clauses are called exception handlers.The The try try code is executed. If all is well, execution continues following code is executed. If all is well, execution continues following any any catch catch clauses present.clauses present.If an exception occurs, processing continues at the first If an exception occurs, processing
View Full Document