我有类似以下情况:
- /** Get a list of records */
- public ArrayList<Record> foo() throws BazException{
- // Create the list
- static ArrayList<Record> records = new ArrayList<Record>();
- // Use MyLibrary to load a list of records from the file
- String str = SomeoneElsesLibrary.loadData(new File("mydata.dat"),new DataLoader(){
- // will be called once for each record in the file
- String processRecord(Record r){
- // if there's no "bar",invalid record
- if( ! r.hasField("bar") ){
- throw new BazException();
- }
- records.add(r);
- }
- });
- return records;
- }
显然这不起作用,因为SomeoneElsesLibrary不知道BazException是什么.我也不能说processRecord()抛出BazException,因为那时原型将不再匹配.我开始认为这个实现的整体结构是错误的. (我正在从Node.JS上瘾中恢复,并且必须重新学习一些大多数Java模式.)我如何重构我的代码以使其成为更惯用的Java?
伪代码很好,甚至只是描述.另外,不要觉得你需要使用一个匿名的内部课程,就像我第一次使用它一样;我只是在寻找“Java方式”来做到这一点.
解决方法
一个异常是指该方法可以抛出它,而不是一个类(SomeoneElsesLibrary).
有两种类型的异常,检查(Exception的子类型)和unchecked(RuntimeException的子类型).必须在可以抛出它的方法的签名中显式声明checked.未经检查的可以传播,而不会在方法的签名中声明,也不会被没有try / catch块处理.
通常检查是在引发异常的方法的调用者可以修复它时使用,否则不选中.
您可以通过try / catch在foo()方法中处理未经检查的异常…
- public ArrayList<Record> foo(){
- static ArrayList<Record> records = new ArrayList<Record>();
- try{
- SomeoneElsesLibrary.loadData( ... );
- } catch (BazException be){ // you just handle the exception here
- }
- return records;
- }
… 或不
- public ArrayList<Record> foo(){
- static ArrayList<Record> records = new ArrayList<Record>();
- // if SomeoneElsesLibrary.loadData raise the BazException
- // it is propagated to the caller of foo()
- SomeoneElsesLibrary.loadData( ... );
- return records;
- }
相反,必须始终处理已检查的异常.