0

I have a weird question about Exception in Java that I am not sure about.

Suppose we have defined a class:

class A extends RuntimeException { ... }

Suppose we have method called C and it calls method that throws A. What is correct?

  1. Inside try-catch

  2. Throw a Runtime Exception.

  3. 1 or 2

  4. None of the above

2 Answers 2

5

Its a RuntimeException, its your choice to catch it or not. Java will not force you to put try-catch or throw it but still if you want to catch it you can.

Whole purpose of RuntimeException is that user is not supposed to prepare for it and therefore java makes it optional for user to catch it or not.

So out of 4 options, answer is "none of above".

Sign up to request clarification or add additional context in comments.

Comments

1

RuntimeException and subclasses thereof are unchecked and do not need to be declared to be thrown or explicitly handled so the answer to the question is none of the above. Checked exceptions (that extend Exception) would need to be declared or handled within a try-catch block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.