General discussion

Locked

Handling Java Exception

By kat97 ·
Hi,

Needed some help in java programming. Thanks

What is the meaning of 'throws NumberFormatExecption' here?
public static void inputInteger() throws NumberFormatException{ ... }

If i write the following statement, when i compile the program will have error saying exception not thrown or declared. Why the above statement doesn't have the same error?
public static void inputInteger() throws Exception{ ... }

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by kat97 In reply to Handling Java Exception

Point value changed by question poster.

Collapse -

by Martin Simons In reply to Handling Java Exception

probably because the throws statement wants to be
specific about what Exception is actually being
thrown. If the actions in the method throws a
NumberFormatException then that is what it must
be declared as throwing. If your method may also
throw another exception (i.e. ParseException) then
you must say
throws NumberFormatException, ParseException
you cannot catch both by saying
throws Exception

Collapse -

by kat97 In reply to

Poster rated this answer.

Collapse -

by Tarzanek In reply to Handling Java Exception

Works for me to throw only Exception ...
however my method is not void nor static
(e.g. public Method(..) throws Exception { ... )

I suppose one of those could be the prob.
Did you try look up for Exception in API, static constraints, void constraints in Language specs regarding to throws keyword ?

http://java.sun.com/reference/docs/index.html

Collapse -

by kat97 In reply to

Poster rated this answer.

Collapse -

by kat97 In reply to Handling Java Exception

This question was closed by the author

Back to Web Development Forum
6 total posts (Page 1 of 1)  

Related Discussions

Related Forums