Java异常处理:缺少返回类型

时间:2016-06-24 23:05:40

标签: java exception-handling return

我对Java比较陌生,我遇到了一个我无法想办法解决的问题。

这是我的代码:

// Ex2 is a child of Ex1
public T method(someType someArg) throws Ex1{
  try{
    someFunc() // Throws both Ex1 and Ex2
    T ret = someOtherFunc() //Throws Ex1, cannot declare/initialize ret outside
    return ret
  } catch(Ex2){
    //do something
  } catch(Ex1){
    //do something
  } 

}

现在java抱怨缺少返回类型,而当我删除Ex2处理/捕获时它不会抱怨。

2 个答案:

答案 0 :(得分:2)

您的方法必须返回public class DataAdapter extends RecyclerView.Adapter { private final int VIEW_ITEM = 1; private final int VIEW_PROG = 0; private List<Student> studentList; // The minimum amount of items to have below your current scroll position // before loading more. private int visibleThreshold = 5; private int lastVisibleItem, totalItemCount; private boolean loading; private OnLoadMoreListener onLoadMoreListener; public DataAdapter(List<Student> students, RecyclerView recyclerView) { studentList = students; if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) { final LinearLayoutManager mLayoutManager = (LinearLayoutManager) recyclerView .getLayoutManager(); recyclerView .addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); totalItemCount = mLayoutManager.getItemCount(); lastVisibleItem = mLayoutManager .findLastVisibleItemPosition(); if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) { // End has been reached // Do something if (onLoadMoreListener != null) { onLoadMoreListener.onLoadMore(); } loading = true; } } }); } } @Override public int getItemViewType(int position) { return studentList.get(position) != null ? VIEW_ITEM : VIEW_PROG; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { RecyclerView.ViewHolder vh; if (viewType == VIEW_ITEM) { View v = LayoutInflater.from(parent.getContext()).inflate( R.layout.list_row, parent, false); vh = new StudentViewHolder(v); } else { View v = LayoutInflater.from(parent.getContext()).inflate( R.layout.progress_item, parent, false); vh = new ProgressViewHolder(v); } return vh; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof StudentViewHolder) { Student singleStudent = (Student) studentList.get(position); ((StudentViewHolder) holder).tvName.setText(singleStudent.getName()); ((StudentViewHolder) holder).tvEmailId.setText(singleStudent.getEmailId()); ((StudentViewHolder) holder).student = singleStudent; } else { ((ProgressViewHolder) holder).progressBar.setIndeterminate(true); } } public void setLoaded() { loading = false; } @Override public int getItemCount() { return studentList.size(); } public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) { this.onLoadMoreListener = onLoadMoreListener; } // public static class StudentViewHolder extends RecyclerView.ViewHolder { public TextView tvName; public TextView tvEmailId; public Student student; public StudentViewHolder(View v) { super(v); tvName = (TextView) v.findViewById(R.id.tvName); tvEmailId = (TextView) v.findViewById(R.id.tvEmailId); v.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(), "OnClick :" + student.getName() + " \n " + student.getEmailId(), Toast.LENGTH_SHORT).show(); } }); } } public static class ProgressViewHolder extends RecyclerView.ViewHolder { public ProgressBar progressBar; public ProgressViewHolder(View v) { super(v); progressBar = (ProgressBar) v.findViewById(R.id.progressBar1); } } } ,您可以添加T作为最后一个语句。或者,修改return null;的可见性(和默认值)

ret

答案 1 :(得分:1)

如果您只是捕获异常并且在处理/记录它们之后没有抛出任何异常,那么该方法正确处理错误但不返回任何内容(繁荣,编译错误)。

如果您要自己处理异常,则需要返回null(或其他合理的无效值)或重新抛出捕获的异常。

如果要重新抛出异常(ex1除外),请确保将其添加到方法签名

例如:

// Ex2 is a child of Ex1
public T method(someType someArg) throws Ex1{
  try{
    someFunc() // Throws both Ex1 and Ex2
    T ret = someOtherFunc() //Throws Ex1, cannot declare/initialize ret outside
    return ret;

  } catch(Ex2 ex2){        
    // either re-throw the exception (or return null instead)
    throw ex2;

  } catch(Ex1 ex1){        
    // or return a null value (you can re-throw the exception instead)
    return null;
  } 
  // you can also return null here if you want to catch/handle both exceptions separately but want to return null in either case
  // return null;
}