• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Static methods

If I have two static methods
method1 - throws an exception
method2 - does not

If method2 calls method1 - how can this compile?
If appears to be compiling with no problems

I thought the calling method would have to have include a try..catch to cover this?
0
mcgettiu
Asked:
mcgettiu
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
kiranhkCommented:
u must be having a the Exception declared in the throws clause of method2 if you are not having a try/catch block.

0
 
GrandSchtroumpfCommented:
if a method throws a subclass of java.lang.RuntimeException, then you don't need to specify it in the declaration.
0
 
GrandSchtroumpfCommented:
Example:  NullPointerException is a subclass of RuntimeException, so you don't need to declare that method2 throws it.


class Test {
 
  static void method1() throws NullPointerException {
    throw new NullPointerException();
  }
 
  static void method2() throws NullPointerException {
    method1();
  }
 
  public static void main(String[] args) {
    method2();
  }
   
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GrandSchtroumpfCommented:
oops the code should have been:

class Test {
 
  static void method1() throws NullPointerException {
    throw new NullPointerException();
  }
 
  static void method2() {
    method1();
  }
 
  public static void main(String[] args) {
    method2();
  }
 
}

note that "main" does not declare the "throws" either.
0
 
sudhakar_koundinyaCommented:
extending GrandSchtroumpf's statement

RuntimeExeption the name itself says if the exception can't be predictable at time of coding then we can throw RuntimeException

some of examples are

Integer.parseInt("hello"); throws a NumberFormatException (extended from RuntimeException)  but there is no need to say the method should throw exception or no need to put that in try -- catch block

String str=null;
str.indexOf("Hello"); throws NullPointerException
Similarly NullPointerException - Null value is also not predictable at compile time - so here is also there is no need to throw NullPointerException

And GrandSchtroumpf's example is perfect

Regards
Sudhakar
0
 
WebstormCommented:
Hi mcgettiu,

>> If appears to be compiling with no problems
If the exception is not a RuntimeException, if the 2 methods are in differents classes, and method1 wasn't throwing any exception when you compiled method2, then try to recompile both classes (delete the .class files to be sure it's recompiled).

0
 
sudhakar_koundinyaCommented:
thanks for accepting :)
0
 
GrandSchtroumpfCommented:
:°)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now