Uncatchable ChuckNorrisException

前端 未结 17 2226
时光取名叫无心
时光取名叫无心 2020-12-02 03:34

Is it possible to construct a snippet of code in Java that would make a hypothetical java.lang.ChuckNorrisException uncatchable?

Thoughts that came to m

17条回答
  •  囚心锁ツ
    2020-12-02 03:50

    No. All exceptions in Java must subclass java.lang.Throwable, and although it may not be good practice, you can catch every type of exception like so:

    try {
        //Stuff
    } catch ( Throwable T ){
        //Doesn't matter what it was, I caught it.
    }
    

    See the java.lang.Throwable documentation for more information.

    If you're trying to avoid checked exceptions (ones that must be explicitly handled) then you will want to subclass Error, or RuntimeException.

提交回复
热议问题