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?
mcgettiuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.