SlideShare a Scribd company logo
Advantages of Exceptions




                           1
Objectives

On completion of this period, you would be
 able to know

• Exception
• Exception handling
• Advantages of exception handling




                                             2
Recap

• In the last class we have studied about
  the various sources of errors




                                            3
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
Exception            Contd..

• Exceptions can be generated by
  • Java run-time system, or they can be
  • Manually generated by your code




                        CM602.56               5
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
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
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
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
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
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
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
Then what is the advantage of having
exception handling feature?




                                       13
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
Advantages of Exception handling
                                          Contd . . .


3. Classification of errors. With java’s multiple
   catch statement facility, we can classify (of
   categorize) the exceptions




                                                        15
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
Quiz

1. Does Java has in built support for exception
  handling

  A. True
  B. False




                       CM602.56                   17
Quiz

2. Exception handling provides safe mechanism
   to throw an error without crash

  A. True
  B. False




                                                18
Frequently Asked Questions

1. Explain about various sources of exceptions
2. What are the advantages of exceptions?




                      CM602.56                   19

More Related Content

Similar to 9 cm604.42

Adv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdfAdv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdfKALAISELVI P
 
Introduction to Java Programming
Introduction to Java Programming Introduction to Java Programming
Introduction to Java Programming Saravanakumar R
 
OOP with Java
OOP with JavaOOP with Java
OOP with JavaOmegaHub
 
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptxchapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptxFiromsaDine
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in JavaVadym Lotar
 
Exception handling
Exception handlingException handling
Exception handlingpooja kumari
 
Exception handler
Exception handler Exception handler
Exception handler dishni
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event HandlingJava Programming
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allHayomeTakele
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.pptJAYESHRODGE
 
Exception handling
Exception handlingException handling
Exception handlingMinal Maniar
 
Exception Handling
Exception HandlingException Handling
Exception HandlingAlpesh Oza
 

Similar to 9 cm604.42 (20)

Adv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdfAdv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdf
 
Introduction to Java Programming
Introduction to Java Programming Introduction to Java Programming
Introduction to Java Programming
 
OOP with Java
OOP with JavaOOP with Java
OOP with Java
 
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
CS3391 -OOP -UNIT – III  NOTES FINAL.pdfCS3391 -OOP -UNIT – III  NOTES FINAL.pdf
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
 
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptxchapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Exception handling
Exception handlingException handling
Exception handling
 
java Features
java Featuresjava Features
java Features
 
Exception handler
Exception handler Exception handler
Exception handler
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event Handling
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Exception
ExceptionException
Exception
 
Event handling
Event handlingEvent handling
Event handling
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.ppt
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

9 cm604.42

  • 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
  • 18. Quiz 2. Exception handling provides safe mechanism to throw an error without crash A. True B. False 18
  • 19. Frequently Asked Questions 1. Explain about various sources of exceptions 2. What are the advantages of exceptions? CM602.56 19