0% found this document useful (0 votes)
35 views10 pages

Exceptions

An exception is an abnormal condition that occurs during runtime. Exceptions are used to signal problems and contain information about the nature of the issue. In Java, exceptions are objects that are created when problems arise. Exception handling allows you to catch exceptions to fix errors and prevent program termination. The try block identifies code that can throw exceptions. Catch blocks catch specific exceptions and allow handling of the exception. Finally blocks execute whether an exception is thrown or not. Nested try blocks allow catching multiple exception types. Exceptions will propagate up the call stack to the nearest matching catch block unless caught earlier. The order of catch blocks must proceed from more specific to more general exception types.

Uploaded by

Qasier_Ali_7928
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
35 views10 pages

Exceptions

An exception is an abnormal condition that occurs during runtime. Exceptions are used to signal problems and contain information about the nature of the issue. In Java, exceptions are objects that are created when problems arise. Exception handling allows you to catch exceptions to fix errors and prevent program termination. The try block identifies code that can throw exceptions. Catch blocks catch specific exceptions and allow handling of the exception. Finally blocks execute whether an exception is thrown or not. Nested try blocks allow catching multiple exception types. Exceptions will propagate up the call stack to the nearest matching catch block unless caught earlier. The order of catch blocks must proceed from more specific to more general exception types.

Uploaded by

Qasier_Ali_7928
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 10

Exception Objects

• An exception is an abnormal condition that


arises in a code sequence at rum time.

• Exception is a way of signaling serious problem.

• An Exception in Java is an Object that’s created


when an abnormal situation arises in your
program.

• The Exception Object has data members that


store information about the nature of the problem.
Exception Handling

• class Test {
public static void main (String[] args) {
int d=0;
int a=10/d;
} } Uncaught Exception.
• java.lang.ArithmeticException: / by zero.

• This Exception is caught by java default handler.


Catching Exception

• It allow you to fix the errors.


• It prevents the program from automatically
terminating.

try and catch Blocks

• Use try block to identify the code that can throw


exceptions.
• Use catch blocks to catch the exceptions.
try and catch Block

• Code generates ArithmeticException.

class Test {

public static void main (String[] args) {


int b=0;

try {
int a=10/b;} scope of a is limited in try block.

catch (ArithmeticException e){


System.out.println(“ Divide by zero Exception”);}
}
}
try and catch Blocks

• Code generates ArithmeticException and


ArrayIndexOutOfBoundsException.

class Test {
public static void main (String[] args) {
int a=10;
int b[] = new int [5];
try {
System.out.println(a/0);
System.out.println(b[5]);}
catch (ArithmeticException e){
System.out.println(“ Divide by zero Exception”);}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(“ Array Index Exception”);}
}}
Nested try Statements

class Test {
public static void main (String[] args) {
int a=10;
int b[] = new int [5];
try{
try {
System.out.println(a/0);
System.out.println(b[5]);}
catch (ArithmeticException e) {
System.out.println(“Divide by zero
Exception”);}
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(“ Array Index Exception”);}
}}
Propagation of Exception

class Test {
public static void main (String[] args) {

Test1 obj1 = new Test1();


try{
obj1.abc();}

catch (ArithmeticException e){


System.out.println(“Divide by zero Exception”);}
}}

class Test1 {
abc() {
System.out.println(10/0); }
}
try, catch and finally

class Test {
public static void main (String[] args) {
Test1 obj = new Test1();
System.out.println(obj.add());
}}

class Test1{
int add(){
try {
System.out.println(10/10);
return 1;}

catch (ArithmeticException e) {
System.out.println("Divide by zero Exception");
return 2;}

finally {
System.out.println("finally block");
return 3;}
}}
try and finally

class Test {
public static void main (String[] args) {
Test1 obj = new Test1();
System.out.println(obj.add());
}}

class Test1{
int add(){
try {
System.out.println(10/10);
return 1;}

finally {
System.out.println("finally block");
return 3;}
}}
Sequence of Catch Blocks

• Illegal Sequence

class Test {
public static void main (String[] args) {
int a[] = new int [5];
try {
System.out.println("abc".substring(3,2));
System.out.println(a[5]);
General Class cannot
}
comes before Special
classes.
catch (IndexOutOfBoundsException e) {
System.out.println(“ Index Exception");}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“ Array Index Exception");}
catch (StringIndexOutOfBoundsException e) {
System.out.println(“ String Index Exception");}
finally {
System.out.println(“ finally block ");}
}}

You might also like