This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can main function use Throws ?  RSS feed

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can main function in a java program use "Throws" to throw an exception ? and where we can handle that exception i.e where to put catch block for that ? kindly explain..
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kunal vermaa wrote:Can main function in a java program use "Throws" to throw an exception ?


What happens when you try?

and where we can handle that exception i.e where to put catch block for that ? kindly explain..


What code do you use to call the main method?
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ofcourse you can use throws clause in main function.

It is just a function like any other funtion in java.But in order to keep it as entry for java program you should altleast follow the below signature.

public static void main(string array)

Generally you run the program using the comand "java ClassName" in such a case JVM will execute the main method inside that class and if main method is declared to throw any exception then it will be handled by JVM by terminating the class abruptly and display the stack trace.

You can also call the main method from another class just like calling any other method in such case you could wrap the call inside try/catch block.

 
kunal vermaa
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for such a detailed explanation..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!