2016-11-21 101 views
0
捕获异常

所以我们可以说我有一个类应用这是我正在做的错误处理,并在此特定的类我做的方法引发内部方法

try { 
     layermethod(); 
    } catch (IOException e) { 
     Message.fileNotFound(filename); 
    } 

layermethod()Layer类,这是一条“掩码“类,我用它将信息从应用程序传递到核心应用程序和对象。

所以在类层layermethod()只被定义为

public void layermethod(Parser _parser) { 
    _parser.throwmethod(_interpreter); 
    } 

只有在解析类我有实际抛出异常

public void throwmethod(Parser _parser) throws IOException { 
     // method implementation 
    } 

不幸的是由于是方法,我得到错误

error: unreported exception IOException; must be caught or declared to be thrown 
     _parser.throwmethod(_parser); 
         ^

因为我没有捕捉到异常在图层类上。

有没有什么办法可以只对App类进行错误处理(捕获异常)?假设我不能丢弃图层类

回答

1

您有两种选择。要么你可以重构Layer.layermethod()也抛出IOException

public void layermethod(Parser _parser) throws IOException { 
    _parser.throwmethod(_interpreter); 
} 

或者,你可以明确地在上面的方法添加try catch块:

public void layermethod(Parser _parser) { 
    try { 
     _parser.throwmethod(_interpreter); 
    } catch (IOException e) { 
     // handle exception here 
    } 
} 

既然你似乎想要把例外进入App班,前一种选择可能是你想到的。