How can I throw an exception for the whole class instead of doing it method by method

孤人 提交于 2019-11-27 03:49:04

问题


I'm writing a program in Java, and nearly every method in one of my classes is written like:

public void doStuff() throws AWTException{}

Is there a way for me to get rid of the extra step of typing throws AWTException for each method, and somehow do it for the entire class?


回答1:


Sorry, No. There is no way to do this in Java.




回答2:


if you throw exception with class level then how you identify that on which type of exception is thrown by method... or on which method it throw exception.

so it is not allow in java...and also its not a good coding with java




回答3:


You can by throw exception at constructor level. You can see how FileInputStream force us to throw IO exception while creating object of it.



来源:https://stackoverflow.com/questions/4405940/how-can-i-throw-an-exception-for-the-whole-class-instead-of-doing-it-method-by-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!