Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

confusion regarding throws clause

  Asked By: Willie    Date: Sep 13    Category: Java    Views: 511
  

i have a confusion regarding throws
cluase in main method's signature .....



we use THROWS in the signature of that method in which we dont want to
catch the exception in the method and let the calling method handle the
exception ... its OK

but why we use THROWS in main like :

public static void main(String[] args) throws anyException

(where anyException is an exception type)

now my confusion starts from here ... as the main () method is called from
the command line ... where is the anyException is going to be handled ........
?

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Darrell Harvey     Answered On: Sep 13

What calls main? Java calls main.

C:\>java myfile
... exceptions are output here.

You could write your own exception  handler if you wish. Lazy
programmers tend to throw everything.

It is always better to catch the error for your own benefit.

public foo() {
try {}
catch(Exception e)
{
e.printStackTrace();
}
}

It even tells you the line  number of the error! How cool is that?

 
Answer #2    Answered By: Bethany Hughes     Answered On: Sep 13

well within the Main method  if the body throws  any exception  then that is
handled by this Throws clause..you can also provide a list of the expected
exceptions separated by commas just within the signature  of the Main...

 
Didn't find what you were looking for? Find more on confusion regarding throws clause Or get search suggestion and latest updates.




Tagged: