在Java中查找异常消息中的特定字符串

时间:2017-11-12 19:49:39

标签: java exception

我有一个带有抛出IOException的方法的库。像,

package helpful.library;

import java.io.IOException;
import org.apache.http.HttpStatus;

public class HelpfulClass{

    public MyObject MyMethod(String a) throws IOException {
        //does some stuff
        return helpfulMethod(String a);
    }

    private MyObject helpfulMethod(String a, int b) throws IOException {
        //does some stuff that includes retrieving a status code x
        if (x != HttpStatus.SC_OK) {
            throw new IOException("blah blah code: " + x);
        } 
        //does stuff and returns a MyObject object
    }

我想从另一个应用程序调用MyMethod,我想检查不是 HttpStatus.SC_OK 的特定状态代码( x in helpfulMethod)。在Java中,最好使用异常消息并检查字符串是否包含我的状态代码? 还有其他方法吗? 一个可能的想法是更改MyObject以引发另一个异常,但这对其他期望仅IOException的应用程序产生连锁反应。

0 个答案:

没有答案