Java如何从try,catch和finally返回值? [英] Java How do you return a value from try,catch, and finally?
本文介绍了Java如何从try,catch和finally返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以当我在try {}中做一个块的代码时,我尝试返回一个值,它出现了不返回值。这是我使用的代码代表我的问题。
import org.w3c.dom.ranges.RangeException;
public class Pg257E5
{
public static void main(String [] args)
{
try
{
System.out.println(add(args));
}
catch(RangeException e)
{
e.printStackTrace();
}
finally
{
System.out.println(感谢使用程序kiddo!);
}
}
public static double add(String [] values)//这里显示一个commpile错误,我没有一个返回值
{
try
{
int length = values.length;
double arrayValues [] = new double [length];
double sum = 0; (int i = 0; i< length; i ++)
{
arrayValues [i] = Double.parseDouble(values [i]);
sum + = arrayValues [i];
}
返还金额//这里有一个返回值。是因为如果一个异常发生在try中的代码,并且没有得到返回值?
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
catch(RangeException e)
{
throw e;
}
finally
{
System.out.println(感谢您使用该程序!); //所以我需要返回一个double类型的返回值这里?
}
}
}
我遇到的问题是在使用try和catch块时如何返回值?
解决方案
要返回使用 try / catch
的值可以使用临时变量,例如
public static double add(String [] values){
pre>
double sum = 0.0;
try {
int length = values.length;
double arrayValues [] = new double对于(int i = 0; i< length; i ++){
arrayValues [i] = Double.parseDouble(values [i]);
sum + = arrayValues [ i];
} catch(NumberFormatException e){
e.printStackTrace();
} catch(RangeException e){
throw e;
} finally {
System.out.println(谢谢你使用程序!);
}
return sum;
}
否则您需要返回在每个执行路径(try block或catch块)中,没有
throw
。So when I do a code of blocks inside a "try{}", and I try to return a value, it comes out " no return values". This is the code I am using that represents my problem.
import org.w3c.dom.ranges.RangeException; public class Pg257E5 { public static void main(String[]args) { try { System.out.println(add(args)); } catch(RangeException e) { e.printStackTrace(); } finally { System.out.println("Thanks for using the program kiddo!"); } } public static double add(String[] values) // shows a commpile error here that I don't have a return value { try { int length = values.length; double arrayValues[] = new double[length]; double sum =0; for(int i = 0; i<length; i++) { arrayValues[i] = Double.parseDouble(values[i]); sum += arrayValues[i]; } return sum; // I do have a return value here. Is it because if the an exception occurs the codes in try stops and doesn't get to the return value? } catch(NumberFormatException e) { e.printStackTrace(); } catch(RangeException e) { throw e; } finally { System.out.println("Thank you for using the program!");// so would I need to put a return value of type double here? } } }
Bascially, the question I'm having is "How do you return a value when you are using try and catch block?
解决方案To return a value when using
try/catch
you can use a temporary variable, e.g.public static double add(String[] values) { double sum = 0.0; try { int length = values.length; double arrayValues[] = new double[length]; for(int i = 0; i<length; i++) { arrayValues[i] = Double.parseDouble(values[i]); sum += arrayValues[i]; } catch(NumberFormatException e) { e.printStackTrace(); } catch(RangeException e) { throw e; } finally { System.out.println("Thank you for using the program!"); } return sum; }
Else you need to have a return in every execution path (try block or catch block) that has no
throw
.这篇关于Java如何从try,catch和finally返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文