• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception: When to throw?  RSS feed

 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have got a question regarding to Exception: Why do some exceptions force the caller method to catch/throw and some don't?

If I do this:

My IDE will make me to have the caller of myMethod to catch or throw the exception I throw here.

However, some methods I have come across previously like Integer.parseInt(), it will throw an exception at runtime (NumberFormatException? or something like that) but it does not require the caller to catch. (Hidden in compile time?)

Why does this so? How do I throw an exception that doesn't force the caller method to catch/throw?

Thank you all in advance!
 
Nagarajan Lakshamanan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The jvm handles the exception if you give "public static void main(String args[]) throws Exception"

caller method can have only the user defined exception.To make understand for the user , the user defined exception are thrown for a specific purpose (like NumberFormatException)
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the difference between checked and unchecked exceptions, the compiler will always report to you any checked exceptions that are not handled.

http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html

e.g.
You may want to create your own exception, let's call it MyException, that extends Exception class. Then write a method that throws MyException. You'll see that the compiler warns you that the exception is not handled. But if you change MyException and make it extend RuntimeException (or any of its subclasses) instead, your exception will not be checked anymore.

Cheers.
Claudiu
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!