0% found this document useful (0 votes)
9 views4 pages

Exception Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Exception Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

How to handle exception in java ?

1)try catch block .


2)throw
3)throws
4)try catch finally.

1) try catch block :

Inside try block we will write risky code.

syntax : try{
-------------
}catch(){
--------------
}

1) Arithmetic Exception.

Example 1: No Exception

package basicsOfException;

public class DiffrentExceptions {

public static void main(String[] args) {


System.out.println("Main method Started");
int a = 10;
int b = 2;
int c = a / b;
System.out.println(c);

System.out.println("Main method Ended");


}
}

Example 2: Exception

package basicsOfException;

public class DiffrentExceptions {

public static void main(String[] args) {


System.out.println("Main method Started");
int a = 10;
int b = 0;
int c = a / b;
System.out.println(c);

System.out.println("Main method Ended");


}
}

Example 3:

package basicsOfException;
public class DiffrentExceptions {

public static void main(String[] args) {


System.out.println("Main method Started");
int a = 10;
int b = 0;
try {
System.out.println("Try Block started");
int c = a / b;
System.out.println(c);
System.out.println("Try Block Ended");
} catch (ArithmeticException e) {
System.out.println("Catch Block");
}
System.out.println("Main method Ended");
}
}

2) Null Pointer Exception.

Example 1:

package basicsOfException;

public class DiffrentExceptions {

public static void main(String[] args) {


System.out.println("Main method Started");

String s = "Santosh";
System.out.println(s.length());
System.out.println("Main method Ended");
}
}

Example 2:

package basicsOfException;

public class DiffrentExceptions {

public static void main(String[] args) {


System.out.println("Main method Started");

String s = null;
System.out.println(s.length());
System.out.println("Main method Ended");
}
}

Example 3:

package basicsOfException;

public class DiffrentExceptions {

public static void main(String[] args) {


System.out.println("Main method Started");

String s = null;
try {
System.out.println(s.length());
}catch(NullPointerException n) {
System.out.println("Catch Block");
}
System.out.println("Main method Ended");
}
}

3) NumberFormat Exception.

Example 1:

package basicsOfException;

public class DiffrentExceptions {

public static void main(String[] args) {


System.out.println("Main method Started");

String s = "123";

int parseInt = Integer.parseInt(s);


System.out.println(parseInt);
System.out.println("Main method Ended");
}
}

Example 2:

package basicsOfException;

public class DiffrentExceptions {

public static void main(String[] args) {


System.out.println("Main method Started");

String s = "123Santosh";

int parseInt = Integer.parseInt(s);


System.out.println(parseInt);
System.out.println("Main method Ended");
}
}

Example 3:

package basicsOfException;

public class DiffrentExceptions {

public static void main(String[] args) {


System.out.println("Main method Started");

String s = "123Santosh";
try {
int parseInt = Integer.parseInt(s);
System.out.println(parseInt);
} catch (NumberFormatException n) {
System.out.println("Catch Block");
}

System.out.println("Main method Ended");


}
}

You might also like