Tuesday, 13 October 2015

Custom exception handling example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
try{
System.out.println("The value is " +i);

if(i==0){
Myexception m = new Myexception("Less than 10");
throw m;
}
}
catch(Myexception m){
m.printStackTrace();

}
finally{
System.out.println("I am in final block");
}
}
}

}


public class Myexception extends Exception {
String j;
Myexception(){
System.out.println("Default constructed");
}
Myexception(String i){
super(i);
}
}

output:-
The value is 0
Myexception: Less than 10
at First.main(First.java:11)
I am in final block
The value is 1
I am in final block
The value is 2
I am in final block
The value is 3
I am in final block
The value is 4
I am in final block
The value is 5
I am in final block
The value is 6
I am in final block
The value is 7
I am in final block
The value is 8
I am in final block
The value is 9
I am in final block

Exception handling example in java (ArrayIndexOutOfBoundsException exception handling)


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second();
obj.S();
}

}



public class Second {
int i[]=new int[5];
   void S(){
try{  i[7]=2;}
catch (ArrayIndexOutOfBoundsException a){
a.printStackTrace();
System.out.println("Catch Block.....");
}
finally{
 
System.out.println("Final Block .....");
}
  
   }
}

output:-
java.lang.ArrayIndexOutOfBoundsException: 7
at Second.S(Second.java:6)
at First.main(First.java:7)
Catch Block.....
Final Block .....


Exception handling example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
try{
int k = args.length;
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
Sec obj = new Sec();
obj.sum(i, j);
}

catch(ArrayIndexOutOfBoundsException a){

a.printStackTrace();
System.out.println("Please pass runtime arguments while executing the program");
}
finally{
System.out.println("Finally block");
}
}
}


public class Sec {
void sum(int i,int j){
int k = i+j;
System.out.println("The value is " +k);
}
}

output:-
java.lang.ArrayIndexOutOfBoundsException: 0
at First.main(First.java:8)
Please pass runtime arguments while executing the program
Finally block

Java Checked Exception example

import java.io.*;
public class First {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("Enter your number");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i = Integer.parseInt(br.readLine());
System.out.println("The value of \"i\" is " +i);
}
}

output:-
Enter your number
10
The value of "i" is 10

Java Interface example


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Third obj = new Second();
Four obj1 = new Second();
obj.F();
obj.S();
obj1.F();
obj1.M();
obj1.S();
obj1.T();
}

}


public class Second implements Four {
public void F(){
System.out.println("F");
}
public void S(){
System.out.println("S");
}
public void T(){
System.out.println("T");
}
public void M(){
System.out.println("M");
}

}



public interface Third {
void F();
void S();
}

interface Four extends Third{
void T();
void M();
}


output:-
F
S
F
M
S
T

Monday, 12 October 2015

Interface example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Third();
obj.display();
obj.fine();
}

}



public interface Second {
void display();
void fine();
}



public class Third implements Second{
public void display(){
System.out.println("One");
}
public void fine(){
System.out.println("Two");
}
}


output:-
One
Two

Abstract class example 2


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Third obj = new four();
obj.First();
obj.like();
obj.display();
}

}


public class Second {
void display(){
System.out.println("Testing display");
}
}

abstract class Third extends Second{
abstract void First();
void like(){
System.out.println("like");
}
}

class four extends Third{
void First(){
System.out.println("Testing Third Display");
}
}

output:-
Testing Third Display
like
Testing display