Thursday, November 6, 2014

Java Exception Pro



MyStaticImportExmp.java

package com.java2novice.stat.imp.pac2;
import static com.java2novice.stat.imp.pac1.MyStaticMembClass.*; 
public class MyStaticImportExmp
{
 public static void main(String a[])
{
 System.out.println("Increment value: "+Increment);
 int count = 10;
 System.out.println("Increment count: "+incrementNumber(count));
 System.out.println("Increment count: "+incrementNumber(count));
    }
}

Java Default Constructor

package com.myjava.constructors;
public class MyDefaultConstructor
{
 public MyDefaultConstructor()
{
        System.out.println("I am inside default constructor...");
    }
    public static void main(String a[])
   {
        MyDefaultConstructor mdc = new MyDefaultConstructor();
    }
}

Java Parameterized Constructor

package com.myjava.constructors;
public class MyParameterizedConstructor
{
 private String name;    
 public MyParameterizedConstructor(String str)
    {
        this.name = str;
        System.out.println("I am inside parameterized constructor.");
        System.out.println("The parameter value is: "+str);
    }
 public static void main(String a[])
{
   MyParameterizedConstructor mpc = new       MyParameterizedConstructor("Madhu Raj");
    }
}

Java Constructor Overloading

package com.myjava.constructors;
public class MyOverloading
{
public MyOverloading()
{
System.out.println("Inside default constructor");
}
public MyOverloading(int i)
{
System.out.println("Inside single parameter constructor with int value");
}
public MyOverloading(String str)
{
System.out.println("Inside single parameter constructor with String object");
}
public MyOverloading(int i, int j)
{
 System.out.println("Inside double parameter constructor");
 }
 public static void main(String a[])
{
        MyOverloading mco = new MyOverloading();
        MyOverloading spmco = new MyOverloading(10);
        MyOverloading dpmco = new MyOverloading(10,20);
    MyOverloading dpmco = new MyOverloading("java2novice");
    }
}

Java Constructor Chaining

package com.myjava.constructors;
public class MyChaining
{
public MyChaining()
{
System.out.println("In default constructor...");
}
public MyChaining(int i)
{
this();
System.out.println("In single parameter constructor...");
}
public MyChaining(int i,int j)
{
        this(j);
        System.out.println("In double parameter constructor...");
 }
 public static void main(String a[])
{
        MyChaining ch = new MyChaining(10,20);
     }
}

Java Singleton

package com.myjava.constructors;
public class MySingleTon
{
private static MySingleTon myObj;
private MySingleTon()
{
}
public static MySingleTon getInstance()
{
        if(myObj == null)
{
            myObj = new MySingleTon();
   }
        return myObj;
    }
   public void getSomeThing()
  {
        System.out.println("I am here....");
  }
  public static void main(String a[])
 {
        MySingleTon st = MySingleTon.getInstance();
        st.getSomeThing();
    }
}

Termination of Program due to abrupt exception

package com.myjava.exceptions;
public class MyException
{
 public static void main(String a[])
{
        for(int i=5;i>=0;i--)
       {
            System.out.println(10/i);
         }
        System.out.println("After for loop...");
    }
}

Java Exception Handling

package com.myjava.exceptions;
public class MyExceptionHandle
{
 public static void main(String a[])
{
        try
{
            for(int i=5;i>=0;i--)
{
 System.out.println(10/i);
            }
        }
catch(Exception ex)
                  {
            System.out.println("Exception Message: "+ex.getMessage());
            ex.printStackTrace();
        }
        System.out.println("After for loop...");
    }
}

No comments:

Post a Comment