Download 23. Exception Handling and more Exercises Logic in PDF only on Docsity!
23. Exception Handling
Java
Fall 2009 Instructor: Dr. Masoud Yaghini
Exception Handling
Outline^ ^ Exception-Handling Overview^ ^ Example: No Exception Handling^ ^ Example: Exception Handling^ ^ When to Use Exception Handling^ When to Use Exception Handling^ ^ References
Exception Handling
Introduction ^ Exception^ –^ an indication of a problem that occurs during aprogram’s execution ^ Exception handling^ –^ resolving exceptions that may occur so program can–^ resolving exceptions that may occur so program can^ continue or terminate well ^ Exception handling enables programmers tocreate programs that are more robust
Exception Handling
Examples ^ ArrayIndexOutOfBoundsExceptionArrayIndexOutOfBoundsExceptionArrayIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException^ –^ an attempt is made to access an element past theend of an array ^ NullPointerExceptionNullPointerExceptionNullPointerExceptionNullPointerException^ –^ when a
nullnull^ reference is used where an object isnullnull
-^ when a
nullnull^ reference is used where an object isnullnull expected
^ InputMismatchExceptionInputMismatchExceptionInputMismatchExceptionInputMismatchException^ –^ occurs when
ScannerScannerScannerScanner
method^
nextIntnextIntnextIntnextInt
receives a
string that does not represent a valid integer ArithmeticExceptionArithmeticExceptionArithmeticExceptionArithmeticException – can arise from a number of different problems inarithmetic
Exception Handling
Performance Tip ^ If the potential problems occur infrequently,intermixing program and error-handling logiccan degrade a program’s performance, ^ Because the program must perform (potentially^ Because the program must perform (potentially^ frequent) tests to determine whether the taskexecuted correctly and the next task can beperformed.
Example: No Exception Handling
Exception Handling
DivideByZeroNoExceptionHandling.java ^ Output 1: Please enter an integer numerator: 100Please enter an integer denominator: 7Result: 100 / 7 = 14
Exception Handling
DivideByZeroNoExceptionHandling.java ^ Output 2: Please enter an integer numerator: 100Please enter an integer denominator: 0 Exception in thread "main" java.lang.ArithmeticException: / by zero^ at DivideByZeroNoExceptionHandling.quotient(DivideByZeroNoExceptionHandling.java:11)^ at^ DivideByZeroNoExceptionHandling.main
(DivideByZeroNoExceptionHandling.java:
23 )
at^ DivideByZeroNoExceptionHandling.main
(DivideByZeroNoExceptionHandling.java:
23 )
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Example: Exception Handling
Exception Handling
Example: Exception Handling ^ With exception handling, the program catchesand handles the exception ^ Next example allows user to try again if invalidinput is entered (zero for denominator, or non-^ integer input)integer input) ^ Example:^ –^ DivideByZeroWithExceptionHandling.java
Exception Handling
DivideByZeroWithExceptionHandling.java ^ Output 2:^ Please enter an integer numerator: 100Please enter an integer denominator: 0Exception: java.lang.ArithmeticException: / by zero^ Zero is an invalid denominator. Please try again.Zero is an invalid denominator. Please try again.^ Please enter an integer numerator: 100Please enter an integer denominator: 7Result: 100 / 7 = 14
Exception Handling
DivideByZeroWithExceptionHandling.java ^ Output 3:^ Please enter an integer numerator: 100Please enter an integer denominator: helloException: java.util.InputMismatchException^ You must enter integers. Please try again.You must enter integers. Please try again.^ Please enter an integer numerator: 100Please enter an integer denominator: 7Result: 100 / 7 = 14
Exception Handling
Catching Exceptions ^ catchcatchcatchcatch
block – catches (i.e., receives) and handles an exception,contains: – Begins with keyword
catchcatchcatchcatch
-^ Exception parameter in parentheses – exception^ parameter identifies the exception type and enablesparameter identifies the exception type and enables^ catchcatchcatchcatch
block to interact with caught exception object
-^ Block of code in curly braces that executes whenexception of proper type occurs
Exception Handling
Catching Exceptions ^ Matching
catchcatchcatchcatch
block
-^ the type of the exception parameter matches thethrown exception type exactly Uncaught exception –^ an exception that occurs for which there are no^ matching
catchcatchcatchcatch^ blocks matching
catchcatchcatchcatch^ blocks