Exceptions in Java

Слайд 2

Consider the following points

Understanding Exceptions
Hierarchy of Exceptions
Types of Exceptions
Handling Exceptions
Throwing Exceptions

Consider the following points Understanding Exceptions Hierarchy of Exceptions Types of Exceptions Handling Exceptions Throwing Exceptions

Слайд 3

Understanding Exceptions

A program can fail for just about any reason:
The code tries

Understanding Exceptions A program can fail for just about any reason: The
to connect to a website, but the Internet connection is down.
You made a coding mistake and tried to access an invalid index in array.
An Exception is Java’s way of saying, “I give up, I don’t know what to do right now. You deal with it”.

Слайд 4

Hierarchy of Exceptions

Hierarchy of Exceptions

Слайд 5

Types of Exceptions

Types of Exceptions

Слайд 6

Runtime Exceptions

ArithmeticException – thrown by the JVM when code attempts to divide

Runtime Exceptions ArithmeticException – thrown by the JVM when code attempts to
by zero.
ArrayIndexOutOfBoundsException – thrown by the JVM when code uses an illegal index to access an array.
ClassCastException – thrown by the JVM when an attempt is made to cast an exception to a subclass of which it is not an instance.
IllegalArgumentException – thrown by the programmer to indicate that a method has been passed an illegal or inappropriate argument.
NullPointerException – thrown by the JVM when there is a null reference where an object is required.
NumberFromatException – thrown by the programmer when an attempt is made to convert a string to a numeric type but the string doesn’t have an appropriate format.

Слайд 7

Checked Exceptions

FileNotFoundException – thrown programmitacally when code tries to reference a file

Checked Exceptions FileNotFoundException – thrown programmitacally when code tries to reference a
that does not exist.
IOException – thrown programmatically when there is a problem reading or writing a file.

Слайд 8

Errors

ExceptionInInitializerError – thrown by the JVM when a static initializer throws an

Errors ExceptionInInitializerError – thrown by the JVM when a static initializer throws
exception and doesn’t handle it.
StackOverflowError – thrown by the JVM when a method calls itself too many times.
NoClassDefFoundError – thrown by the JVM when a class that the code uses is available at compile time but not runtime.

Слайд 9

Handling Exceptions

Handling Exceptions

Слайд 10

Throwing Exceptions

A method that generates an unhandled exception is said to throw an exception:
It

Throwing Exceptions A method that generates an unhandled exception is said to
generates an exception to signal an exceptional condition
A method it calls throws an exception