• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can main function use Throws ?  RSS feed

 
kunal vermaa
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..
 
Steve Luke
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?
 
Siva Masilamani
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!