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

Java异常处理:缺少返回类型,java,exception-handling,return,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

我对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处理/捕获时它没有抱怨。

您的方法必须返回
T
,您可以添加
返回null作为最后一条语句。或者,修改可见性(和默认值)
ret

public T method(someType someArg) throws Ex1{
  T ret = null;
  try{
    someFunc(); // Throws both Ex1 and Ex2
    ret = someOtherFunc();
  } catch(Ex2){
    //do something
  } catch(Ex1){
    //do something
  } 
  return ret;
}

您的方法必须返回一个
T
,您可以添加
returnnull作为最后一条语句。或者,修改可见性(和默认值)
ret

public T method(someType someArg) throws Ex1{
  T ret = null;
  try{
    someFunc(); // Throws both Ex1 and Ex2
    ret = someOtherFunc();
  } catch(Ex2){
    //do something
  } catch(Ex1){
    //do something
  } 
  return ret;
}

如果您只是捕获异常,并且在处理/记录异常后没有抛出任何异常,那么该方法正确地处理了错误,但没有返回任何内容(boom、编译错误)

如果要自己处理异常,则需要返回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;
}

如果您只是捕获异常,并且在处理/记录异常后没有抛出任何异常,那么该方法正确地处理了错误,但没有返回任何内容(boom、编译错误)

如果要自己处理异常,则需要返回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;
}

但是当我删除Ex2的catch块时,代码就会编译,只处理Ex1。我想了解这种行为。@FayazAhmed然后将代码隐藏在这些catch块中是个好主意。但一般来说:您需要一个
return
语句,或者通过抛出异常来中止该方法
catch Ex2
不支持后者,因此它需要一个
返回值
catch Ex1
重新抛出异常,或者抛出一个新异常。但是当我删除Ex2的catch块时,代码会编译,只处理Ex1。我想了解这种行为。@FayazAhmed然后将代码隐藏在这些catch块中是个好主意。但一般来说:您需要一个
return
语句,或者通过抛出异常来中止该方法
catch Ex2
不支持后者,因此它需要一个
返回值
<代码>捕获Ex1
重新抛出异常或抛出新异常。异常是在运行时发生的。您遇到的是编译错误。异常是在运行时发生的。您遇到的是编译错误。