recent

Java Program that Implement Throw and Throws

Write a Java Program that Implement Throw and Throws:-

class ThrowDemo
{
ThrowDemo()
{
try
{ throw new NullPointerException();
}
catch(NullPointerException e)
{
System.out.println("Caught in constructor");
throw e;
}
}
public static void main(String args[])
{
try{
ThrowDemo td=new ThrowDemo();
}
catch(NullPointerException e)
{
System.out.println("Caught in Main");
}
}
}
class ThrowsDemo
{
ThrowsDemo() throws NullPointerException
{ System.out.print("in constructor");
throw new NullPointerException();
}
public static void main(String args[])
{
try{
ThrowsDemo td=new ThrowsDemo();
}
catch(NullPointerException e)
{
System.out.println("Caught in Main");
}
}
}
Powered by Blogger.