简体   繁体   中英

Questions about Java Exception

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

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".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM