December 8, 2021 Exception Handling in Java 1
December 8, 2021 Exception Handling in Java 1
Exception
NumberFormatException
EOFException FileNotFoundException InterruptedIOException
All
Common
exceptions
IOException Exception
caninherit sub
fromclasses
be decomposeda base
intoinclude:
class Exception
specific classes of I/O errors
NumberFormatException
December 8, 2021 is a Handling
Exception subclass of DataFormatException
in Java 24
Exception Handling
Example
Consider implementing the BufferReader readLine( ) example with
try-catch clocks.
import java.io.*
public class ExceptionalExample {
public static void main(String [ ] args) //no throws clause used here
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println(“Enter a number”);
try {
try block encapsulates
String str = br.readLine( ); the readLine( ) method
double num = Double.parseDouble(str); and the conversion from
}
December 8, 2021 Exception Handling in Javaif str
String to double. 25
Will throw NumberFormatException
cannot be converted to a double.
Exception Handling
Example (continued)
• FileNotFoundException ( java.io )
– Request to open file fails
• IllegalArgumentException ( java.lang )
– Method passed illegal / inappropriate argument
• IOException ( java.io )
– Generic I/O error
• NullPointerException ( java.lang )
– Attend to access object using null reference
• UnsupportedOperationException ( java.lang )
– Object does not provide requested operation