how to throw exception..?
class A
{
void execute()throws Exception
{
int i=10/0;
}
publicstaticvoid main(String args[])
{
try
{
new A().execute();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
(or)
class A
{
void execute()throws Exception
{
try
{
int i=10/0;
}
catch(Exception e)
{
throw e;
}
}
publicstaticvoid main(String args[])
{
try
{
new A().execute();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
hi i want to knw which form of throwing of exception is best from the above two program... and also tell me the reason pls... can anyone give the solution.....
[2212 byte] By [
83Krisha] at [2007-11-27 10:39:48]

for me:
class A
{
void execute() throws Exception{
int i=10/0;
}
public static void main(String args[]){
try{
new A().execute();
} catch(Exception e) {
System.out.println(e);
}
}
}
why? the other has redundant try catch.
There are two general classes of exceptions in java, often called checked and unchecked.
The exception thrown by zero divide, in your example, is a RuntimeException and doesn't require a throws clause.
Most exceptions are throw with a throw statement, but a few kinds are throw directly by the JVM, zero divide or NullPointerExceptions being examples.
If you need to throw an exception you typically create an exception class of your own, by extending the Exception class. That way you can have a catch clause that catches just that one kind of exception and your program can act accordingly.