What is exception
An unwanted event/situation that disturbs normal flow of program is called exception.
OR
A situation Occurs where our program get terminated
Exception Handling
-----------------
A situation Occurs where our program get terminated so rest program is not executed
in oreder to execute this program
we have to handle it
By using try catch Block we can handle
Exception
Thats is Called Exception Handling
We cant Repair Exception
But We can Handle it with help try and catch
Exception Handling
try
catch
throws
finally
throw
try{
// Risky Buggy Guarded Code
}catch()
{
}
What is the Hierachy Of Exception
Object
|
Throwable
|
---------------------------------
Exception Error
| |
|- IOException |-StackOverFlowError
|- SQLException |-VirtualMachineError
|- ClassNotFoundException |-OutOfMemoryError
|-RunTimeException
|
|- ArithmaticException
|-NullPointerException
|- IndexOutOfBoundException
| |
| | -ArrayIndexOutOfBoundException
Error
-----
+ Errors are non recoverable
+ Errors Occur due to lack of system resorces
+ Ex- VMError, StackOverFlowError,OutOfMemoryError
package p2;
// java.lang.StackOverflowError
public class Demo {
int j;
int k;
int m;
public static void main(String[] args) {
try {
// main(args);
} catch (Exception e) {
}
}
}
(RumtimeException are implicitely propageted)
Exception are of 2 Types:
+Checked Exception :
--------------------------
* These are the exception checked at compile time.
*Its mandatoty to hadle
+Unchecked Exception :
---------------------
[is a exception which are unable to checkk at compile time]
These are occured at run time.
All RunTimeExceptions are Unchecked Exception
Checked Exception
File f=new File("ABC.txt");
f.createNewFile();
// Note
error: unreported exception IOException; must be caught or declared to be thrown
f.createNewFile();
*** Problem *****
package p3;
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException{
f1();
}
private static void f1() throws IOException {
File f=new File("ABC.txt");
/*try {
f.createNewFile();
}catch(IOException e) {
}*/
f.createNewFile();
System.out.println("Cool");
}
}
*************************************
package p3;
import java.io.File;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
/*
try {
f1();
}catch(InterruptedException | IOException e)
{
}
*/
try {
f1();
}catch (Exception e) { // Generic Exception
}
}
private static void f1() throws InterruptedException, IOException {
Thread.sleep(5000); //InterrputedException
File f=new File("ABC.txt");
f.createNewFile(); // IOException
System.out.println("Cool");
}
}
***********************************
We can use throws keyword with function
for ex
private static void f1() throws InterruptedException, IOException
we can throws multiple Exception.
*************************************
throw
------
By using throw we can rethrow exception
for ex throw new ArithmeticException();
and we can throw only one exception at a time.
Best Case to Use Throw keyword to create our
own Exception
---------------------------------------------
finally
-------------
It is Block
This Block Always Executes.
** we have to use finally with
try with finally
try{
}finally{
}
or
try catch finally
try{
}catch(){
}finally{
}
Only one case finally block not executed
System.exit(0);
----------------------------------------------
try with Resources
package p5;
import java.util.InputMismatchException;
import java.util.Scanner;
// try with resources
public class Test {
public static void main(String[] args) {
int n;
/*
* Scanner sc=new Scanner(System.in); n=sc.nextInt(); System.out.println(n);
*/
try(Scanner sc1=new Scanner(System.in);){
System.out.println("Enter num");
n=sc1.nextInt();
System.out.println(n);
}catch (InputMismatchException e) {
}
}
}
************************************
Exception Propagation Means
Delegate Responsibility of exception to calller
*******Customized Exception***********
package p6;
import java.util.Scanner;
class InvalidAge extends RuntimeException {
public InvalidAge(String s) {
super(s);
}
}
public class Test {
public static void main(String[] args) {
int age;
System.out.println("Enter Age");
Scanner sc = new Scanner(System.in);
age = sc.nextInt();
checkAge(age);
}
private static void checkAge(int age) {
if (age < 18) {
try {
throw new InvalidAge("Invalid Age Below 18");
} catch (InvalidAge e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("Perfect Age");
}
}
}
Comments
Post a Comment