Wednesday, March 11, 2009

Exception Example(Throw and Throws )

class MyExp extends Exception
{
public MyExp(String msg)
{
System.out.println(msg);
}
}

public class throws1 {
static int mtd(int a,int b) throws MyExp
{
if(b==0) throw new MyExp("Example for throw b") ;
else
if(a==0) throw new MyExp("Example for throw a") ;
return a/b;
}
public static void main(String args[])
{
try
{
System.out.print(mtd(10,0));
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Exception Example

Exception Example

Exception Example

Tuesday, March 3, 2009

Constructor Example

public class constructor_ex {
public constructor_ex()
{
System.out.print("Constructor example");
}
public static void main(String args[])
{
constructor_ex obj=new constructor_ex();
}
}
/*constructor is a method in a class ,that has same name as that of a class.
* constructor don't have return type ,not even void.
* They automatically intialize objects with default values , when they are created.
* They are invoked using new operator .
* They can't be inherited .
* "This" and "super" can be used .
*/