Catch a generic exception in Java?

前端 未结 7 1175
不知归路
不知归路 2020-12-11 14:26

We use JUnit 3 at work and there is no ExpectedException annotation. I wanted to add a utility to our code to wrap this:

 try {
     someCode();         


        
7条回答
  •  旧时难觅i
    2020-12-11 14:58

    Generics are not types. They are not templates. They are compile time type checks, in Java. Exception blocks catch on type. You can catch(Exception e) or even catch(Throwable e) and then cast as needed.

提交回复
热议问题