Java 方法给出错误,使方法返回无效

Java 方法给出错误,使方法返回无效,java,android,Java,Android,它显示错误: make方法CreateStep返回语句void 为什么?如果发生异常,您必须返回一些内容: public Step createStep(int position) { try { final StepFragmentSample step = new StepFragmentSample(); Bundle b = new Bundle(); b.putInt(CURRENT_STEP_POSITION_KEY, posi

它显示错误:

make方法CreateStep返回语句void


为什么?

如果发生异常,您必须返回一些内容:

public Step createStep(int position) {
    try {
        final StepFragmentSample step = new StepFragmentSample();
        Bundle b = new Bundle();
        b.putInt(CURRENT_STEP_POSITION_KEY, position);
        step.setArguments(b);
        return step;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("/Test", "/Excp due to" + e.toString());
    }
}


您必须始终返回步骤对象。如果代码中有异常,您将永远不会返回Step对象

试着这样做:

public Step createStep(int position) {
    try {
        final StepFragmentSample step = new StepFragmentSample();
        Bundle b = new Bundle();
        b.putInt(CURRENT_STEP_POSITION_KEY, position);
        step.setArguments(b);
        return step;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("/Test", "/Excp due to" + e.toString());
    }
    return null;
}
像这样做

public Step createStep(int position) {
    try {
        final StepFragmentSample step = new StepFragmentSample();
        Bundle b = new Bundle();
        b.putInt(CURRENT_STEP_POSITION_KEY, position);
        step.setArguments(b);
        return step;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("/Test", "/Excp due to" + e.toString());
    }
    return new Step(); // or return null;
}

一旦方法被执行,您需要使用void或返回一些东西。但是我返回步骤,然后它为什么会给出错误?应该在catch语句之后调用return谢谢sir@Jens@萨加尔:很高兴见到您,先生,我可以在这里以外的地方与您联系吗?邮件还是其他什么
public Step createStep(int position) {
    try {
        final StepFragmentSample step = new StepFragmentSample();
        Bundle b = new Bundle();
        b.putInt(CURRENT_STEP_POSITION_KEY, position);
        step.setArguments(b);
        return step;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("/Test", "/Excp due to" + e.toString());
    }
    return new Step(); // or return null;
}
public Step createStep(int position) {
try {
    final StepFragmentSample step = new StepFragmentSample();
    Bundle b = new Bundle();
    b.putInt(CURRENT_STEP_POSITION_KEY, position);
    step.setArguments(b);

} catch (Exception e) {
    e.printStackTrace();
    Log.e("/Test", "/Excp due to" + e.toString());
}
 return step;
}