An exception is a problem
that arises during the execution of a program. Exception interrupt the normal
flow of the program and the program/Application terminates abruptly
Types
of Exception
As per sun microsystem there are three type of
exception -
1.
Checked Exception
2.
Unchecked Exception
3.
Error
Checked Exception
Checked
exception are mandatory to handle otherwise they will give time error like
IOException, SQLException etc.
Code
Snippet –
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* Example of checked exception
* @author Manoj
*/
public class Exception {
public static void main(String[] args) {
try {
FileInputStream fileInputStream = new FileInputStream(new File("test.txt"));
byte array[] = new byte[2014];
fileInputStream.read(array);
fileInputStream.close();
} catch (IOException e) { //IOException need to capture or
throws
e.printStackTrace();
}
}
}
Unchecked Exception
Checked
exception are not mandatory to handle means compiler will not give any error.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. are
unchecked exceptions.
Error
Error
is irrecoverable like OutOfMemoryError, VirtualMachineError, AssertionError
etc.
Create custom
exception-
Checked Exception –
To create checked exception
extends Exception class
Unchecked Exception –
To create unchecked exception extends runtime
exception class.
Code
snippet-
/**
* Custome Exception creation
* @author Manoj
*/
public class CustomExceptionTest {
public static void main(String[] args) {
try {
new
CheckedException().createException();
} catch (CheckedException e) {
e.printStackTrace();
}
new UnCheckedException().createException();
}
}
class CheckedException extends Exception {
private static final long serialVersionUID = 1L;
public void createException() throws CheckedException {
throw new CheckedException();
}
}
class UnCheckedException extends RuntimeException {
private static final long serialVersionUID = 1L;
public void createException() {
throw new UnCheckedException();
}
}
Note: - We can’t throw checked exception inside
a lambda expression but we can unchecked exception.
Refer below code snippet:-
public void unCheckedException(){
Map<Integer, Integer> dataMap = new HashMap<>();
dataMap.forEach( (a,b) -> { throw new ArithmeticException();});
}
public void checkedException(){
Map<Integer, Integer> dataMap = new HashMap<>();
dataMap.forEach( (a,b) -> { throw new Exception();}); //compile time error Unhandled
exception type Exception
}
public void checkedExceptionHandled(){
Map<Integer, Integer> dataMap = new HashMap<>();
dataMap.forEach( (a,b) -> { try {
throw new Exception();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}});
}
Throws exception after return
will give compile time error:
public static int method() throws Exception {
try {
return 1;
throw new NullPointerException(); //Unreachable code compiler error
} catch (Exception exc) {
throw new Exception();
}
}
Nice information, very usefull thanks
ReplyDelete