Types of exceptions

15
http:// improvejava.blogspot.in/ 1 Types of Exceptions

description

 

Transcript of Types of exceptions

Page 1: Types of exceptions

http://improvejava.blogspot.in/ 1

Types of Exceptions

Page 2: Types of exceptions

http://improvejava.blogspot.in/ 2

Objectives

On completion of this period, you would be able to learn:• Exception hierarchy

• Types of exceptions

Page 3: Types of exceptions

http://improvejava.blogspot.in/ 3

Recap

• In the last class, you have studied about the concept of multi-catch statements

• We have also written a sample program using multi catch statements

• A program on finally block was also examined

Page 4: Types of exceptions

http://improvejava.blogspot.in/4

Exception HierarchyThe following diagram shows exception hierarchy

Throwable

Error Exception

RuntimeException

Examples

(Out of memory error

Stack overflow errors)

Examples (ArithmeticException, NullpointerException, IndexOutofBoundsException)Fig. 46.1 Exception hierarchy

Page 5: Types of exceptions

http://improvejava.blogspot.in/ 5

Exception Hierarchy Contd ..

• All exception types are subclasses of the built-in class Throwable

• Throwable is at the top of the exception class hierarchy as shown in the Fig 46.1

• Below Throwable are two subclasses that partition exceptions into two distinct branches, namely• Exception

• Error

Page 6: Types of exceptions

http://improvejava.blogspot.in/ 6

Exception Hierarchy Contd ..

• Exception class is used for exceptional conditions that user programs should catch

• It is used to create your own custom exception types • RuntimeException is a subclass of Exception• It is automatically defined for the programs that you

write• Includes things such as division by zero and invalid

array indexing

Page 7: Types of exceptions

http://improvejava.blogspot.in/ 7

Error

• It defines exceptions that are not expected to be caught under normal circumstances by your program

• Stack overflow is an example of such an error

Page 8: Types of exceptions

9CM604.46 8

Type of Exceptions

• There are two types of exceptions in Java• Unchecked exceptions

• Checked exceptions

8http://improvejava.blogspot.in/

Page 9: Types of exceptions

http://improvejava.blogspot.in/ 9

Unchecked Exceptions

• The compiler does not check to see if a method handles or throws these exceptions• Hence the name unchecked

• They need not be included in any method’s throws list

Page 10: Types of exceptions

http://improvejava.blogspot.in/ 10

Checked Exceptions

• The compiler checks whether these exceptions were handled in the method

• That must be included in a method’s throws list• Compiler error occurs if these exceptions were not

handled by the methods

Page 11: Types of exceptions

http://improvejava.blogspot.in/ 11

Unchecked Exceptions Examples

Table 46.1 Unchecked Exception

Page 12: Types of exceptions

http://improvejava.blogspot.in/ 12

Checked Exceptions Examples

Table 46.2 Checked Exception

Page 13: Types of exceptions

http://improvejava.blogspot.in/ 13

Summary

• All exception types are subclasses of the built-in class Throwable

• Throwable is at the top of the exception class hierarchy

• Exception, Error are subclasses of Throwable• Types of exception

• Unchecked exceptions

• Checked exceptions

Page 14: Types of exceptions

http://improvejava.blogspot.in/ 14

Quiz

1. Which exceptions types are not expected to be caught under normal circumstances by your program

A. ErrorB. ExceptionsC. None

Page 15: Types of exceptions

http://improvejava.blogspot.in/ 15

Frequently Asked Questions

1. Explain about the various types of Exceptions

2. List out the various checked and unchecked exceptions