Exception handling in Java has several advantages:
1. Exceptions are built-in classes in Java, allowing for object-oriented exception handling.
2. Exception handling separates the logic of a program from the error handling code, making programs easier to understand.
3. Java allows for classifying exceptions through multiple catch blocks, categorizing different types of errors.
2. Objectives
On completion of this period, you would be
able to know
• Exception
• Exception handling
• Advantages of exception handling
2
3. Recap
• In the last class we have studied about
the various sources of errors
3
4. Exception
• Exception is an abnormal condition that arises
in a code sequence at run time
• In other words, exception is a run-time error
• A Java exception is an object that describes an
error condition that has occurred in a piece of
code
CM602.56 4
5. Exception Contd..
• Exceptions can be generated by
• Java run-time system, or they can be
• Manually generated by your code
CM602.56 5
6. Exception Contd..
• Exceptions are thrown by Java if rules of Java
languages are violated
• Manually generated exceptions are used to
report error condition to the caller of a method.
CM602.56 6
7. Example :
The following example illustrates the possible
source of exceptions
• Suppose you want to write a program to copy a file
contents to another file
• Simply the source file is copied to destination file
CM602.56 7
8. Example : Contd . . .
The following basic steps are needed
1. Accept the source file name
2. Find the size of source file
3. Allocate in buffer memory space for copying read sorce
4. Create the destination file
5. Read the destination from source into buffer
6. Write the contents from buffer to destination file i.e.,
copy the contents
7. Repeat the above steps until file copy is done
8. Close the source and destination files
CM602.56 8
9. Example : Contd . . .
In each above steps, there in possibility of getting
exception
1. The source file name you enter may be wrong
2. The file is big that you cannot represent the file size with
even ‘long’ data type
3. Memory is not available for allocation to buffer
4. You may not have permission to create a file or the
destination folder is write protected
CM602.56 9
10. Example : Contd . . .
5. The sectors you are reading may be bad
6. The sectors in which you are writing are
corrupted
7. While repeating also, any of the above
exceptional connections may occurs
8. While closing the files, errors may occurs
• some body has deleted the specified file (in
multiuser environment )
CM602.56 10
11. Exception handling
• The process of identifying the exception and
performing the appropriate remedy is called as
exception handling
• Java manages exception handling mechanism
by using five keywords: try, catch, throw, throws,
and finally
11
12. Advantages of Exception handling
• Generally, every language has a feature of
handling errors
• For example, in ‘C’ language
• You have handling errors with a sequence of if
statement and function all
12
13. Then what is the advantage of having
exception handling feature?
13
14. Advantages of Exception handling
1. Java has Exception as its built – in class
(a class of java long package)
• Implies, object orientation is supported
1. Separation of logic of program from its
exception handling
• Logic of the program is understood easily
14
15. Advantages of Exception handling
Contd . . .
3. Classification of errors. With java’s multiple
catch statement facility, we can classify (of
categorize) the exceptions
15
16. Summary
• A Java exception is an object that describes an
exceptional or error condition
• Java application never crash as there is strong
exception handling mechanism
• Advantages are
• Exception is a built-in class in Java
• Separation of logic of program for error handling
section
• Classification of exceptions possible
CM602.56 16
17. Quiz
1. Does Java has in built support for exception
handling
A. True
B. False
CM602.56 17