Definition-

Generally, Exception disturbs the normal flow of program.
In java, Exception is an object, which is created when exceptional condition occurs.

– it contains all the information about the exception(type/class of exception and location in the code)

Note- Java includes default handler.

Exceptions hierarchy-
xptios

-[TYPES OF EXCEPTIONS]-

1-CHECKED EXCEPTION:– These are checked by compiler for handling at compile-time.

i.e- IOExeption, FileNotFoundException, ClassNotFoundException, SQLException etc.

2-UNCHECKED EXCEPTION:– These are not checked by compiler for handling at compile time.

i.e- ArithmaticException, NullPointerException, ArrayIndexOutOfBoundException etc.

[Difference bw Checked and Unchecked]
1- C should be solved for compile time, Uc for RunT
2-C should be handled using try-catch or throws, Uc are not required to be handled using try-catch or throws.
3- C are not caused by code but other resources whereas Uc are caused by poor/bad programming.
4- custom C can be created by extending Ex class or any its sub classes except runtime exception, custom Uc by RTEx class

-[5 KEYWORDS USED IN EXCEPTION HANDLING]-

1- TRY

2-CATCH

3-FINALLY

4-THROW

5-THROWS

[]-TRY, CATCH AND FINALLY BLOCK:-

Try block includes the suspected lines of code which can occur exception. Try block is used within the method, and catch block is used just after try block.

Catch block is used to handle the exception.

Finally block is a block that is always executed, and used to perform important tasks that must be performed.

-[scenarios]-

[a]- Try with catch:-

try{

} catch(Exception_class_Name reference) {

}

[b] try with multiple catch blocks-
try {
}catch(Ex1 e){


} catch (Ex2 e) {


}
[c]multiple catch

try{
}catch(IOExc | SQLEx){
}

[d] try with finally-
try {
}finally {
}

[e] – try, catch, finally-
try {
}catch(Ex e){
}finally {
}

try with resource ##

Before java7-
finally {
close() methods
}
After java7-
Don’t need to do that

[] RULES:-

1:- Although multiple catch can be written but only one catch block is executed at a time.

2:- All catch blocks must be ordered from most specific to most general to prevent from complie-time error i.e Execution (general) must come after ArithmeticException(specific)

3:- For each try block there can be only one finally block.

[]:- Finally block is executed whether:-

[a]- exception doesn’t occur

[b]- exception occurred but not handled

[c]- exception occurred and handled

NOTE– Finally block will not be executed if program exits (either by calling System.exit() or by causing a fatal error that causes the process to abort).

#Avoiding a Exception using throws keyword#
#throws- its a keyword, used to declare the exception with method signature.
It can be applied on multiple exceptions at a time.
Syntax- method_sign throws class_name
i.e-void m() throws Exception {      }

#Throw- Its a keyword, used to generate an exception inside the method.
It can only be applied on 1 exception at a time.
Syntax– throw class_instance
i.e- throw new SomeException ()

Code samples are here

1- Checked exception handling with try catch

package exceptionHandling;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

try {
FileInputStream f = new FileInputStream("c://a.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

2-Checked exception handling with try and multiple catch blocks

package exceptionHandling;

import java.io.*;

public class Extc1 {

public static void main(String[] args) {

try {
Class.forName("Array.A");
FileReader fr = new FileReader("D://h.txt");
} catch (ClassNotFoundException e) {

} catch (FileNotFoundException e) {

}

}

}
 3-Checked exception handling with try and multiple catch(from java 7)
package exceptionHandling;

import java.io.*;

public class Extc2 {

public static void main(String[] args) {

try {
Class.forName("Array.A");
FileReader fr = new FileReader("D://h.txt");
} catch (ClassNotFoundException | FileNotFoundException e) {

}
}

}

4- Checked exception handling with try catch finally

package exceptionHandling;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

try {
FileInputStream f = new FileInputStream("c://a.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally{
System.out.println("abc");
}
}

5-   Checked exception handling with throws
package exceptionHandling;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ExChkdEx3 {
public static void main(String[] args) throws FileNotFoundException {
FileInputStream f = new FileInputStream("c://a.txt");
}
}
6- unchecked exception
package exceptionHandling;
public class UnChkdEx {
public static void main(String[] args) {
int a=10;
int b=0;
int c;
c = a / b;
System.out.println(c);
  }
}
Advertisements