Java Examples
OOPS |
Exception |
RMI |
String |
Thread |
io |
Util |
Net |
AWT |
JDBC |
|
|
| EXCEPTION PROGRAM: |
| 1.Try And Multicatch Program |
// Unchecked Runtime Exception :
public class TryMulCatch {
public static void main(String args[]) {
float i=10,k;
try {
float j = Float.valueOf(args[0]).floatValue();
k=i/j;
System.out.println(" The Result of k = i / j is : "+k);
}catch(ArithmeticException e) {
System.out.print("Exception caught : ");
System.out.println(e);
}catch(ArrayIndexOutOfBoundsException a) {
System.out.print("Exception caught : ");
System.out.println(a);
}
}
}
|
| 2.Nested Try Program |
/* NESTED TRY STATEMENT */
public class NestedTry {
public static void main(String args[]) {
try {
int a=Integer.valueOf(args[0]).intValue();
int b=42/a;
try {
int c[]={1};
int k = 10/0; // go for outer catch
c[42]=99;
}catch(ArrayIndexOutOfBoundsException ai){
System.out.println("In Inner Catch :"+ai);
}
}catch(ArithmeticException ae) {
System.out.println("In Outer Catch :"+ ae);
}
System.out.println("After the Exception caught in the catch clause :");
}
}
|
| 3.Finaly Without Catch Program |
/* FINALLY WITH OUT CATCH CLAUSE */
public class Finally {
static void A() {
try {
System.out.println("Inside Method A");
throw new NullPointerException("demo");
}
finally {
System.out.println("Inside A's finally");
}
}
static void B() {
try {
System.out.println("Inside Method B");
return;
}
finally {
System.out.println("Inside B's finally");
}
}
static void C() {
try {
System.out.println("Inside Method C");
}finally {
System.out.println("Inside C's finally");
}
}
public static void main(String args[]) {
try {
A();
}catch(Exception e) {
System.out.println("Exception caught : Inside Main Method ");
}
B();
C();
}
}
|
| 4.User Defined Exception |
class MyException extends Exception {
MyException(String message) {
super(message);
}
}
public class UserDefException {
public static void main(String args[]) {
int x=5, y= 1;
try {
float z= (float) x/(float) y;
if(z < 0.01)
{
throw new MyException("This is User Defined Exception : Number is too small");
}
}catch (MyException e) {
System.out.print("Exception Occured : ");
System.out.println(e.getMessage());
}
finally {
System.out.println("I am in Finally Block ");
}
}
}
|
|
Back to Top |