• Post Reply Bookmark Topic Watch Topic
  • New Topic

should I have a try statement in the main method if I have throws in called method if other class?  RSS feed

 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a noob question,

Consider I have main method from Class1 which calls static method of Class2 that throws ArithmeticException.

Is it required to write for try statement in method from Class1 since I declared a throws statement in the method of Class2?

Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in the case of ArithmeticException, because it is an unchecked exception (it extends java.lang.RuntimeException).

Only in the case of checked exceptions it is required to either catch and handle the exception, or add a "throws" to the method that is calling a method that might throw a checked exception.

See The Catch or Specify Requirement.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should probably not have a try‑catch in a main method at all, because the main method is intended for starting the application only; you should put the try‑catch in a different method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!