1
Roll No: CS-1317
BS-Computer Science
4th Semester
2
Topic:
Exceptional Handling
 What is Exception?
 Definition of Exception Handling?
 Why this occur in program?
 Why we use Exception Handling?
 Its Benefits?
 Catching Exceptions.
 Syntax
 Example
4
 Dictionary Meaning: Exception is an abnormal
condition.
5
 Exception Handling is a mechanism to handle
runtime errors such as ClassNotFound, IO,
SQL, etc.
 An exception is a problem that arises during
the execution of a program.
6
 A user has entered invalid data.
 A file that needs to be opened cannot be
found.
 Abnormal termination of program.
 Some exceptions occurs by physical resources
that have failed in some manner (system
dependent events).
7
 Exception Handling normally use to avoid
disrupts (Break up) the normal flow of the
application that is why we use exception
handling.
8
 The core advantage of exception handling is
to maintain the normal flow of the
application.
9
 A method catches an exception using a
combination of the try and catch keywords.
 A try/catch block is placed around the code
that might generate an exception.
 Code within a try/catch block is referred to
as protected code.
10
1. try
2. {
3. //Protected code
4. }
5. catch(ExceptionName Object)
6. {
7. //Catch block
8. }
11
The following is an array is declared with 2
elements. Then the code tries to access the 3rd
element of the array which throws an exception.
12
1. import java.io.*;
2. public class ExcepTest{
3. public static void main(String args[]){
4. try{
5. int a[] = new int[2];
6. System.out.println("Access element three :" +
a[3]);
7. }
8. catch(ArrayIndexOutOfBoundsException e)
9. {
10. System.out.println("Exception thrown :" + e);
11. }
12. System.out.println("Out of the block");
13. }
14. }
13
 http://www.tutorialspoint.com/java/java_exceptions.htm
 http://www.javatpoint.com/exception-handling-in-java
14
15
16

Exceptional Handling in Java

  • 1.
  • 2.
    Roll No: CS-1317 BS-ComputerScience 4th Semester 2
  • 3.
  • 4.
     What isException?  Definition of Exception Handling?  Why this occur in program?  Why we use Exception Handling?  Its Benefits?  Catching Exceptions.  Syntax  Example 4
  • 5.
     Dictionary Meaning:Exception is an abnormal condition. 5
  • 6.
     Exception Handlingis a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, etc.  An exception is a problem that arises during the execution of a program. 6
  • 7.
     A userhas entered invalid data.  A file that needs to be opened cannot be found.  Abnormal termination of program.  Some exceptions occurs by physical resources that have failed in some manner (system dependent events). 7
  • 8.
     Exception Handlingnormally use to avoid disrupts (Break up) the normal flow of the application that is why we use exception handling. 8
  • 9.
     The coreadvantage of exception handling is to maintain the normal flow of the application. 9
  • 10.
     A methodcatches an exception using a combination of the try and catch keywords.  A try/catch block is placed around the code that might generate an exception.  Code within a try/catch block is referred to as protected code. 10
  • 11.
    1. try 2. { 3.//Protected code 4. } 5. catch(ExceptionName Object) 6. { 7. //Catch block 8. } 11
  • 12.
    The following isan array is declared with 2 elements. Then the code tries to access the 3rd element of the array which throws an exception. 12
  • 13.
    1. import java.io.*; 2.public class ExcepTest{ 3. public static void main(String args[]){ 4. try{ 5. int a[] = new int[2]; 6. System.out.println("Access element three :" + a[3]); 7. } 8. catch(ArrayIndexOutOfBoundsException e) 9. { 10. System.out.println("Exception thrown :" + e); 11. } 12. System.out.println("Out of the block"); 13. } 14. } 13
  • 14.
  • 15.
  • 16.