public String getStr(String str){
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
} catch(Exception e){
}
return "null";
}
为什么这有两个return.很郁闷..... 我想应试是这样;public String getStr(String str){
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
} catch(Exception e){
}
} 可是改成这样又错了.
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
} catch(Exception e){
}
return "null";
}
为什么这有两个return.很郁闷..... 我想应试是这样;public String getStr(String str){
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
} catch(Exception e){
}
} 可是改成这样又错了.
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
//这里发生异常,以下return将不会被执行,异常将会被抛出
String temp=new String(temp_t);
return temp;
} catch(Exception e){
//这里捕获所有异常,包括上面可能抛出的异常,catch以外方法以内的其他逻辑继续执行
}
//这里逻辑继续执行,如果没有return方法将不符合返回String的定义,所以在这里需要再定义返回null
return "null";
}
{
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e){}
return "null";
}
}public class funargs_run{ public static void main(String args[])
{
funargs f1 = new funargs(); String stra;
stra= null;
stra=f1.getStr(stra);
System.out.println(stra);
}}
return temp;
这个return是当执行完try且没错误之后返回的
如果try有错误,就捕获到异常执行catch
然后就执行return "null";返回很正常~~~~~~~
第二个的话,如果在try中没有错的话,理论上是没问题的~~~~~
但有错就必须有返回~~~~~~所以是错的~~~~
为了保证错过返回段,即在return temp前异常跳出
所以在最后返回null是有道理的,最好用个
finally{return "null";}
就更好了
String temp="";
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
temp=new String(temp_t);
} catch(Exception e){
}
return temp;
}
大家看看这种写法对吗?
第二种写法catch中缺少 return
-----------------------------------IT者--IT开发者的网站 8万篇技术资料
www.itzhe.cn
第二种写法catch中缺少 return
-------------------------------------------------
疑问?有没有权威的证据?
当try {...}中语句出错时,由于catch中没有做任何处理,而函数要返回值,即最后那一句return返回null。如果try {...}中语句不出错,返回的是try中return temp。
*注意程序的逻辑
第一个程序的逻辑没有错误
第二个程序在逻辑上就错了,当try中语句出错时,函数不返回任何值,就会出错的,这在java中是不被允许的。java程序为了安全,是不允许第二种程序的
建议楼主仔细看看异常的产生与处理方式!
但是,如果在执行try时,出现异常,则第一个return不会执行到,这时必须有一个return返回string。
其实我也有疑问,如果第一个try执行顺利,则会返回第一个return的值,而第二个return执行时,又返回了值,那哪个是正确的值呢?第一个?第二个?
String temp_p=str;
try{
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
} catch(Exception e){
out.println("something error");
return null;
}
finally
{
return temp;
}
}
longmarch2008() 你的这种思想我明白了....
你这个程序调试过没有,我试了,在JB里面还有问题................
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
} catch(Exception e){
}
return "null";
}
这种程序的书写方式 是 个很不好的习惯!!!!
应该避免之
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
} catch(Exception e){
}
return "null";
} public String getStr(String str){
String temp="";
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
temp=new String(temp_t);
} catch(Exception e){
}
return temp;
} 都是对的。不过没有作异常处理
public String getStr(String str)
{
String temp_p=str;
try
{
byte[] temp_t=temp_p.getBytes("ISO-8859-1");
String temp=new String(temp_t);
}
catch(Exception e)
{
System.out.println("something error");
return null;
}
finally
{
return temp;
}
} }
String temp=null;
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
} catch(Exception e){
temp=null;
}
return temp;
}
这样好像更清晰一点。
程序的第一个可能出口在第一个return 处,所以必须要有return
第二个可能出口在第二个return处,所以同样必须要有return
要考虑程序可能运行到哪结束
try{
//...
return ...
}catch(Exception e){
return ...
}
hehe....
一般是return null;
又return "null"這樣會不會被後面的覆蓋??
第一个return执行的时候已经返回了, 方法的调用到此结束, 那么第二个return 肯定没有执行!
try {
return new String(temp.getBytes("iso-8859-1"));
} catch (Exception e) {
return null;
}
}2. public String getStr(String temp) {
try {
return new String(temp.getBytes("iso-8859-1"));
} catch (Exception e) {
throw new RuntimeException("null");
}
}
3. public String getStr(String temp) {
try {
return new String(temp.getBytes("iso-8859-1"));
} catch (Exception e) {
}
return null;
}
string msg = null;
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
msg="真确";
}
catch(Exception e){
msg = "错误";
}
finally{
return msg;
}
} 这样无论如何都会有返回拉
public String getStr(String str)
{
String temp_p=str;
try
{
byte[] temp_t=temp_p.getBytes("ISO-8859-1");
String temp=new String(temp_t);
}
catch(Exception e)
{
System.out.println("something error");
return null;
}
finally
{
return temp;
}
} }这个写法就有问题了,finally是无论如何都要执行的.如果在try中发生异常,程序将会返回temp就是形参的值.和程序的本意不符合.finally画蛇添足了.
public String getStr(String str)
{
String temp_p=str;
try
{
byte[] temp_t=temp_p.getBytes("ISO-8859-1");
String temp=new String(temp_t);
}
catch(Exception e)
{
System.out.println("something error");
return null;
}
finally
{
return temp;
}
} }
本人对此方法比较喜欢,但不要乱用finally(),因为它有时会出现我们想不到的错误.他会丢失异常的。