当尝试时,最后使用组合,如果有一个return语句,请尝试.为什么最后是块执行?
class Palindrome
{
public static void main(String args[])
{
System.out.println(Palindrome.test());
}
public static int test()
{
try {
//return 0;
return 100;
}
finally {
System.out.println("finally trumps return.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,请告诉我执行的流程.我知道最终将在try块之后强制执行.但是在try块中,返回staatement会将控件带到主类.在那种情况下,控制将如何最终阻止?
我会认为finally只有 a 的子句pass是毫无意义的。
但在 Bottle 模板中,如果没有它,以下可选包含代码将无法工作。
结果将包含之前的所有内容以及所包含的代码本身,但之后不包含任何内容。
(参见相应的问题。)
try:
include(optional_view)
except NameError:
pass
finally:
pass
Run Code Online (Sandbox Code Playgroud)
有什么finally: pass作用,什么时候有用?
我有以下代码:
void func()
{
try
{
f1()
}
finally()
{
if (resource != null)
resource.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我放了三个断点:在finally块中的if语句中调用f1(),f1()内部.
似乎finally块中的断点没有被击中.如果在点击f1()内的断点后,我使用下一个命令,我确实最终通过finally块.
此外,如果我在finally块的两行上放置断点,则第一个断点确实被击中.
不确定这是否是由于JDB中的错误造成的.有人可以告诉我这是一个已知问题还是我错过了什么?
我在Linux上使用JDK 6.
我有一个foreach循环,尝试捕获,最后.它通过文件夹中的文件,最后将它们全部删除....但现在,如果我有两个文件,因为它删除所有(我需要删除所有因为应用程序功能)它不会通过第二个文件.有可能让foreach循环完成然后删除foreach方法中的所有文件这里是一个例子:
foreach (DirectoryInfo directory in directories)
{
foreach (FileInfo file in directory.GetFiles("*.csv"))
{
try
{
//do something with file
}
catch (Exception e)
{
//catch exception
}
finally
{
if (!IsFileLocked(file))
{
string[] files = Directory.GetFiles(@"C:\Test");
foreach (string filetoDelete in files)
{
File.Delete(filetoDelete);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望它足够清楚.
我有这样的条件
String str = null;
try{
...
str = "condition2";
}catch (ApplicationException ae) {
str = "condition3";
}catch (IllegalStateException ise) {
str = "condition3";
}catch (Exception e) {
str = "condition3";
}
if(str == null){
str = "none";
}
Run Code Online (Sandbox Code Playgroud)
现在我想总结一下str = "condition3";.最后,块总是运行,以至于无法满足我的需求.还有什么可以做的.
我有一个简单的疑问.在以下两个代码中,第一个return语句放在一个finally块内
public int method1(){
try{
// Some Stuff
} catch(Exception e){
e.printStackTrace();
} finally{
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
并在第二个return声明中正常放置
public int method1(){
try{
// Some Stuff
} catch(Exception e){
e.printStackTrace();
} finally{
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别吗?哪个可以作为更好的选择?为什么?
这给我带来了一个至高无上的新手问题,但我无法在googleverse的任何地方找到答案.我写了一些相当复杂的脚本,但从未想过这个:
try { raise-errorCode}
catch { do-someThing}
finally {more-code}
Run Code Online (Sandbox Code Playgroud)
与:
try { raise-errorCode}
catch { do-someThing}
more-code
Run Code Online (Sandbox Code Playgroud)
有什么不同?
如果将要执行catch块之后的语句,那么java中finally块的真正用途是什么?例
try {
//Code
}
catch (Exception e)
{
//Code
}
finally {
System.out.println("anyway it will be executed");
}
System.out.println("anyway it will be executed");
Run Code Online (Sandbox Code Playgroud) 我已经运行了客户端-服务器交互的代码。忽略线程部分,我知道那行不通。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
public class Client {
public static void main(final String[] args) throws Exception {
Socket socket = null;
while (true) {
try {
socket = new Socket("localhost", 3456);
System.out.println("Connect Ho gaya");
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
final PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
final BufferedReader sr_receive = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Thread sendMes = new Thread(new Runnable() {
public void run() {
while (true) {
try {
String send = br.readLine(); …Run Code Online (Sandbox Code Playgroud) 通常这些关键字让我感到困惑.
谁能告诉我他们到底有什么区别?