public boolean connectTest(String ip, int port)
{
Socket nodeSocket = null;
try
{
nodeSocket = new Socket(ip, port);
} catch (Exception e)
{
System.out.println("Cannot Connect with Server:" + e);
return false;
}
try
{
nodeSocket.close();
} catch (IOException e)
{
System.out.println("system cannot close socket" + e.getMessage());
return false;
}finally
{
return true;
}
}看了这代码?, 才发现博士原来也可以不用太博学...
{
return true;
}
public boolean connectTest(String ip, int port) {
Socket nodeSocket = null;
try {
nodeSocket = new Socket(ip, port);
} catch (Exception e) {
System.out.println("Cannot Connect with Server:" + e);
return false;
}
try {
nodeSocket.close();
} catch (IOException e) {
System.out.println("system cannot close socket" + e.getMessage());
return false;
} //finally {
return true;
// }
}
catch 语句中后面加上fianlly的话就是要强制执行。。不管怎么样最后都要运行里面的命令在怎么样也都是 true啊!
写了5年JAVA,自考本科的JAVA模拟卷,不及格!!!
感觉像初中时期考政治!!!
研究生毕业和专科毕业差别就在于牌子比人大,年纪比人大,钱拿的比人多!!!
{
return true;
}
这段很值得商榷?!
呵呵,开个玩笑,纯为赚分.
有些人,会交际,却没能力. 交际 == 成功
有些人,有能力,却没关系. 关系
syetem.out.println{"读书+交际+关系=",读书+交际+关系};
public boolean connectTest(String ip, int port) {
return true;
}他太牛了,写了这么多 ,结果返回的和我的 一样!!
int a = 0;
try {
a = 1;
throw new Exception();
} catch (Exception e) {
System.out.println("catch_开始");
return a; } finally {
System.out.println("finally_开始" + a);
a = 3;
System.out.println("finally_完毕" + a);
}
} public static void main(String[] args) {
TestFinally t = new TestFinally();
System.out.println("得到返回的值" + t.doPrint());
}}看看他的执行结果吧catch_开始
finally_开始1
finally_完毕3
得到返回的值1由此可见,这个博士写的代码并没错,反而是对finally有深刻认识才能写出的
这个帖子有点扯
应改为if(nodeSocket!=null){
nodeSocket.close();
}
不然第一个异常后 第二个接着空异常
为啥博士毕业就必须要很熟练的能写JAVA代码?