我对这个尝试,捕获以及最后使用return语句工作流程毫无疑问......
此功能用于检索主管视图的员工休假信息.它工作得很好,但是如果找到if语句的数据,它将返回,否则块将返回.即使两者都获得了回报,它也会最终声明.我不知道为什么?
这里的代码片段:
List<Leave> ILeaveData.GetLeaveForSupervisorView(int userID)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("Storeprocedurename", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int));
cmd.Parameters["@Id"].Value = userID;
// Get employee leave information
try
{
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
List<Leave> leave = new List<Leave>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
leave.Add(CreateLeaveForAdminViewFromDataRow(ds.Tables[0].Rows[i]));
}
return leave; // if data found then statement return …Run Code Online (Sandbox Code Playgroud) 作为这方面的新手,finally在异常处理中使用该子句有什么好处.或者换句话说,当最好使用它时,最好不要使用它.
我能想到的唯一一个是关闭输入/输出流......任何其他好处??? !!
我想知道下面的代码根据 Java 标准是否正确。return语句可以放在try块之后吗?或者将 return 语句放在finally 块中总是好的。请帮我解决这个问题
public int method()
{
try{
//code
return 1;
}
catch(Exception e){
return 0;
}
finally{
}
}
Run Code Online (Sandbox Code Playgroud) 所以我知道finally无论异常如何都会执行一段代码,但不使用它会不好?我一直在使用try/catch,想知道这是不是很糟糕.或者它真的不重要吗?
我是 Promises 的新手,我不确定为什么使用 bluebird 会出现此错误:
.finally 不是函数
我的代码结构如下所示:
const Promise = require('bluebird');
worker();
function worker(){
Promise.try(
do something ...
.then(
do something ...
.then(
do something ...
.then(
do something ...
)
.catch(log.error)
)
.catch(log.error)
)
.catch(log.error)
.finally(() => {
worker().delay(5000);
})
)
}
Run Code Online (Sandbox Code Playgroud)
我很感激在这个问题上的任何帮助。
很长一段时间,我认为它可以让我释放finally块中的所有资源,并且我认为如果try块中发生异常,那么该块中的资源仍然会被释放finally。但情况似乎并非如此。
我有以下一段代码:
using System;
public sealed class Program
{
public static void Main()
{
try {
int zero = 0;
int i = 1/zero;
} finally {
Console.WriteLine("divide by zero"); //the line is never called
}
}
}
Run Code Online (Sandbox Code Playgroud)
我从来没有到达打印到控制台的行。这意味着finally在这种情况下,我将无法在块内抛出异常时释放块中的资源try。
所以,我相信有两件事:要么我遗漏了什么,要么try+finally组合在 C# 中没有用例。第二个语句是有道理的,因为我将获得与上面的代码和下面的代码产生的相同的功能:
using System;
public sealed class Program
{
public static void Main()
{
int zero = 0;
int i = 1/zero;
Console.WriteLine("divide by zero"); //the …Run Code Online (Sandbox Code Playgroud) 我正在学习 Clojure,我想创建一个像 Javas try-catch 一样工作的宏。如果抛出异常,则应返回异常。否则,应返回结果。任何打开的资源也应该在 finally 部分关闭(不使用 with-open)。但是,我确实有一个问题,除了没有被抓住,我不知道如何解决。任何帮助或建议将不胜感激!
这是我的代码:
(defmacro my-try
([expression]
`(try
~expression
(catch Exception e# e#)
)
)
([[value variable] expression]
`(let [~value ~variable]
(try
~expression
(catch Exception e# e#)
(finally (if (instance? java.io.Closeable ~value) (.close ~value)))
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
当我尝试打开并读取不存在的文件时:
(def v (my-try [s (FileReader. (File. "missing-file"))] (. s read)))
(println v)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Execution error (FileNotFoundException) at java.io.FileInputStream/open0 (FileInputStream.java:-2).
missing-file (Det går inte att hitta filen)
Run Code Online (Sandbox Code Playgroud)
我怀疑这个错误信息是一个未捕获的异常,因为(1)它没有遵循其他异常的结构,以及(2)如果我改变了异常返回的格式,比如添加了特定的消息,错误信息仍然如此没变。
非常感谢任何帮助找出问题所在!非常感谢您考虑我的要求。
一旦我执行下面的代码,媒体播放60秒,我的应用程序关闭,出现错误"不幸的是,YourAPP已经停止." .如果我删除"bv.setImageResource(R.drawable.play);" 在最后阻止,该应用程序完美.
bv.setImageResource(R.drawable.play)在finally块上有什么问题?
这是我的代码
bv = (ImageButton) findViewById(R.id.imageButton1);
private void mpsleep() {
mp.start();
Thread timer= new Thread(){
public void run(){
try{
for (i =1;i<60;i++){
sleep(1000);//1 second pause
}
}catch(Exception e){
e.printStackTrace();
}finally{
mp.pause();
mp.seekTo(0);
bv.setImageResource(R.drawable.play);
}
}
};
timer.start();
}
Run Code Online (Sandbox Code Playgroud)
logcat的
03-06 11:03:39.743: V/MediaPlayer(31802): message received msg=4, ext1=0, ext2=0
03-06 11:03:39.743: V/MediaPlayer(31802): Received seek complete
03-06 11:03:39.743: V/MediaPlayer(31802): All seeks complete - return to regularly scheduled program
03-06 11:03:39.743: V/MediaPlayer(31802): callback application
03-06 11:03:39.743: V/MediaPlayer(31802): back from callback
03-06 …Run Code Online (Sandbox Code Playgroud) public boolean sendDeviceEvent() {
boolean status = false;
try {
device.sendEvent("blah...blah");
status = true;
} catch (Exception e) {
log.error("Failed to send NodeLowBattery Event - {} {}", createNodeLowBatteryNotification(), e.getCause());
} finally {
return status;
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道上面的代码如何被认为是不好的做法,因为它从最后返回.根据字节码信息,最后不会突然返回,最后没有设置值.怎么会被认为是坏事?
假设我有一些像这样的C#代码:
try {
Method1();
}
catch(...) {
Method2();
}
finally {
Method3();
}
Method4();
return;
Run Code Online (Sandbox Code Playgroud)
我的问题是,只要没有异常被抛出,将方法3()方法4将()之前执行呢,还是该finally块只有之前执行return,continue或break声明?