If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.
You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Try to understand the different in throws and throw keywords.
The following method declares that it throws a Remote Exception:
// errord :
import java.io.*;
public class abc
{
public int disp(int age) throws invalidageException
{
if (age>18)
{
system.out.println("valid voter");
}
else
{
throw new invalidageException("not valid");
}
}
public static void main (String arg[])
{
abc x= new abc();
x.disp(15);
}
}
You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Try to understand the different in throws and throw keywords.
The following method declares that it throws a Remote Exception:
// errord :
import java.io.*;
public class abc
{
public int disp(int age) throws invalidageException
{
if (age>18)
{
system.out.println("valid voter");
}
else
{
throw new invalidageException("not valid");
}
}
public static void main (String arg[])
{
abc x= new abc();
x.disp(15);
}
}
No comments:
Post a Comment