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]
# 1

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.

Yannixa at 2007-7-28 19:03:13 > top of Java-index,Java Essentials,Java Programming...
# 2

how to throw exceptions:

http://java.sun.com/docs/books/tutorial/essential/exceptions/throwing.html

Yannixa at 2007-7-28 19:03:13 > top of Java-index,Java Essentials,Java Programming...
# 3

if you throw the exception out side the method (execute), then don't use try cathch statement in side that method. because try catch methods will handle that exception. so it can't be throw out side this method.

jeyrama at 2007-7-28 19:03:13 > top of Java-index,Java Essentials,Java Programming...
# 4

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.

malcolmmca at 2007-7-28 19:03:13 > top of Java-index,Java Essentials,Java Programming...