尝试抓住问题 [英] Try Catch question

查看:47
本文介绍了尝试抓住问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个fucntion中有一个try catch语句应该返回一个真正的
或者一个假的


我的代码如下所示:


尝试

mySqlConnection.Open()

Dim Da1 As New SqlDataAdapter(" Select JnlType,Description from

JnlType",mySqlConnection)

Dim Ds As New DataSet(" X")

Da1.Fill(Ds)

Catch

MsgBox(填充数据集查找表时出现问题

表示JnlType)

返回错误

最后

如果mySqlConnection.State = ConnectionState.Open那么

mySqlConnection.Close()

结束如果

结束尝试


我的问题是:Try Catch代码是否足够聪明,可以在MsgBox之后运行Finally

语句然后返回false或者我是需要以其他方式编写

代码。这就是我想要它做的但是我不确定我是否正确地写了它的b $ b。

解决方案

< blockquote>最后将永远执行,是的,它仍然会以你写的方式返回false

" Woody Splawn" < WO *** @ splawns.com>在留言中写道

news:u7 ************** @ TK2MSFTNGP10.phx.gbl ...

我试试看声明中的声明应该返回
true或false

我的代码如下所示:

尝试
mySqlConnection.Open()
Dim Da1 As New SqlDataAdapter(" Select JnlType,Description
from JnlType",mySqlConnection)
Dim Ds As New DataSet(" X")
Da1.Fill(Ds) )
Catch
MsgBox(填充数据集进行查询
表有JnlType的问题)
返回错误
最后
如果mySqlConnection。 State = ConnectionState.Open然后
mySqlConnection.Close()
结束如果
结束尝试

我的问题是:Try Catch代码是否足够智能运行
最后在MsgBox之后的语句然后返回false或者我需要编写
代码以其他方式。这就是我想要它做的但是我不确定我b $ b是否正确写了它。



William ...我认为我们需要澄清一些事情。由于他的写作方式,它不会因为
返回FALSE。它将返回FALSE

,因为当没有设置返回值时它会结束FALSE。

Woody ......我不认为你想要它只是"偶然"工作。测试它

你自己但是(作为测试的一部分)有一个函数返回一个整数,所以你

可以得到超过2个返回值。

除非我大错特错,否则它的工作方式如下:


您需要在代码中使用两个return语句:在Try块中返回True

并在结束尝试后返回False。在Catch或最后没有返回。


如果没有错误,它将返回Try块中的值(TRUE)如果

出现错误Catch块执行,最后你将

返回Return语句的值(FALSE)。


有趣的是即使你发出一个return语句在Catch区块中它将不会返回
。要么返回工作,要么最终返回。


测试出来......

Tom


William Ryan eMVP <双** @ NoSp4m.devbuzz.com>在消息中写道

新闻:uF ************** @ TK2MSFTNGP09.phx.gbl ...

最后将永远执行是的,它仍然会以你写的
方式返回false。
Woody Splawn < WO *** @ splawns.com>在消息中写道
新闻:u7 ************** @ TK2MSFTNGP10.phx.gbl ...

我在一个函数中有一个try catch语句应该返回


true

或者假

我的代码如下:

试试
mySqlConnection.Open()
Dim Da1 As New SqlDataAdapter(" Select JnlType,Description


from

JnlType",mySqlConnection)
Dim Ds As New DataSet(X)
Da1.Fill(Ds)
Catch
MsgBox(填充数据集进行查找时出现问题

table

for JnlType")
返回False
最后
如果mySqlConnection.State = ConnectionState.Open那么
mySqlConnection.Close()
结束如果结束尝试

我的问题是这样的: Try Catch代码是否足够聪明,可以在MsgBox之后运行


最后

statment然后返回false或者我需要以其他方式编写
代码。这就是我想要它做的但是我不确定我


它写得正确。




汤姆:


好​​的电话,是的,你是绝对正确的。我意识到我的回答可能有误导 - 我正在回答它是否有例外情况

导致b / c似乎他在询问行为最后。但是,如果你没有在这个例子中返回一个值,它将实际上返回false。

这更像是VB.NET的一个功能可以让你不要在一个函数中明确地返回

a值但是你仍然有一个好点。


下次我会更清楚。


bill

Tom Leylan < ge*@iamtiredofspam.com>在消息中写道

新闻:eO ************** @ TK2MSFTNGP12.phx.gbl ...

威廉......我相信我们需要澄清一些事情。由于他写作的方式,它不会返回FALSE。它将返回FALSE
因为当没有设置返回值时它结束为FALSE。

Woody ......我不认为你只是想要偶然工作。自己测试它,但是(作为测试的一部分)有一个函数返回一个整数,所以
你可以得到超过2个返回值。

除非我错了很多它的工作原理如下:

你需要在代码中使用两个return语句:在Try块中返回True
并在结束尝试后返回False。在Catch或Finally中没有返回。

如果没有错误,它将返回Try块中的值(TRUE)如果Catch块执行错误,则最后并且你将返回Return语句的值(FALSE)。

有趣的是即使你在Catch块中放置一个return语句它也不会被返回。要么返回工作,要么最后返回

测试它......


William Ryan eMVP <双** @ NoSp4m.devbuzz.com>在消息中写道
新闻:uF ************** @ TK2MSFTNGP09.phx.gbl ...

最后将永远执行,是的,它仍然会返回false


方式

你写的。
Woody Splawn < WO *** @ splawns.com>在消息中写道
新闻:u7 ************** @ TK2MSFTNGP10.phx.gbl ...

我在一个函数中有一个try catch语句应该返回


a

true

或false

我的代码如下所示:

尝试
mySqlConnection.Open()
Dim Da1作为新的SqlDataAdapter(" Select JnlType,Description


来自

JnlType",mySqlConnection)
Dim Ds As New DataSet(" X")
Da1.Fill(Ds)
Catch
MsgBox(" There填充数据集进行查找


for JnlType")
返回错误
最后
如果mySqlConnection.State = ConnectionState 。打开然后
mySqlConnection.Close()
结束如果结束尝试

我的问题是:Try Catch代码是否足够智能以运行


最后

以后的语句MsgBox然后返回false或者我需要以其他方式编写


代码。这就是我想要它做的但是我不确定我


它写得正确。





I have a try catch statement in a fucntion that is supposed to return a true
or a false

My code looks like this:

Try
mySqlConnection.Open()
Dim Da1 As New SqlDataAdapter("Select JnlType, Description from
JnlType", mySqlConnection)
Dim Ds As New DataSet("X")
Da1.Fill(Ds)
Catch
MsgBox("There was a problem filling the Dataset for Lookup table
for JnlType")
Return False
Finally
If mySqlConnection.State = ConnectionState.Open Then
mySqlConnection.Close()
End If
End Try

My question is this: Is the Try Catch code smart enough to run the Finally
statment after the MsgBox and then return false or do I need to write the
code some other way. This is what I want it to do but I am not sure I have
it written correctly.

解决方案

Finally will always be executed and yes, it will still return false the way
you wrote it.
"Woody Splawn" <wo***@splawns.com> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...

I have a try catch statement in a fucntion that is supposed to return a true or a false

My code looks like this:

Try
mySqlConnection.Open()
Dim Da1 As New SqlDataAdapter("Select JnlType, Description from JnlType", mySqlConnection)
Dim Ds As New DataSet("X")
Da1.Fill(Ds)
Catch
MsgBox("There was a problem filling the Dataset for Lookup table for JnlType")
Return False
Finally
If mySqlConnection.State = ConnectionState.Open Then
mySqlConnection.Close()
End If
End Try

My question is this: Is the Try Catch code smart enough to run the Finally statment after the MsgBox and then return false or do I need to write the
code some other way. This is what I want it to do but I am not sure I have it written correctly.



William... I believe we need to clarify something. It isn''t going to
return FALSE because of the way he wrote it. It is going to return FALSE
because it ends up FALSE when no return value has been set.
Woody... I don''t think you want it to just "accidentally" work. Test it
yourself but (as part of the test) have a function return an integer so you
can get more than 2 return values.

Unless I''m greatly mistaken it works as follows:

You want two return statements in your code: Return True in the Try block
and Return False after the End Try. With no return in Catch or Finally.

If there is no error it will return the value in the Try block (TRUE) if
there is an error the Catch block executes, the the Finally and you will
return the value of the Return statement (FALSE).

Interestingly even if you place a return statement in the Catch block it
will not be returned. Either the Try return works or the final return does.

Test it out...
Tom

"William Ryan eMVP" <bi**@NoSp4m.devbuzz.com> wrote in message
news:uF**************@TK2MSFTNGP09.phx.gbl...

Finally will always be executed and yes, it will still return false the way you wrote it.
"Woody Splawn" <wo***@splawns.com> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...

I have a try catch statement in a fucntion that is supposed to return a


true

or a false

My code looks like this:

Try
mySqlConnection.Open()
Dim Da1 As New SqlDataAdapter("Select JnlType, Description


from

JnlType", mySqlConnection)
Dim Ds As New DataSet("X")
Da1.Fill(Ds)
Catch
MsgBox("There was a problem filling the Dataset for Lookup


table

for JnlType")
Return False
Finally
If mySqlConnection.State = ConnectionState.Open Then
mySqlConnection.Close()
End If
End Try

My question is this: Is the Try Catch code smart enough to run the


Finally

statment after the MsgBox and then return false or do I need to write the code some other way. This is what I want it to do but I am not sure I


have

it written correctly.




Tom:

Good call, and yes you''re absolutely correct. I realize my answer may have
been misleading -- I was answering it in the context of if an exception was
caused b/c it seemed he was asking about the behavior of Finally. However,
if you don''t return a value in this instance, it will in fact return false.
That''s more a function of how VB.NET can allow you to not explicitly return
a value in a function but you have a good point nonetheless.

I''ll be more clear next time.

bill
"Tom Leylan" <ge*@iamtiredofspam.com> wrote in message
news:eO**************@TK2MSFTNGP12.phx.gbl...

William... I believe we need to clarify something. It isn''t going to
return FALSE because of the way he wrote it. It is going to return FALSE
because it ends up FALSE when no return value has been set.
Woody... I don''t think you want it to just "accidentally" work. Test it
yourself but (as part of the test) have a function return an integer so you can get more than 2 return values.

Unless I''m greatly mistaken it works as follows:

You want two return statements in your code: Return True in the Try block
and Return False after the End Try. With no return in Catch or Finally.

If there is no error it will return the value in the Try block (TRUE) if
there is an error the Catch block executes, the the Finally and you will
return the value of the Return statement (FALSE).

Interestingly even if you place a return statement in the Catch block it
will not be returned. Either the Try return works or the final return does.
Test it out...
Tom

"William Ryan eMVP" <bi**@NoSp4m.devbuzz.com> wrote in message
news:uF**************@TK2MSFTNGP09.phx.gbl...

Finally will always be executed and yes, it will still return false the


way

you wrote it.
"Woody Splawn" <wo***@splawns.com> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...

I have a try catch statement in a fucntion that is supposed to return

a

true

or a false

My code looks like this:

Try
mySqlConnection.Open()
Dim Da1 As New SqlDataAdapter("Select JnlType, Description


from

JnlType", mySqlConnection)
Dim Ds As New DataSet("X")
Da1.Fill(Ds)
Catch
MsgBox("There was a problem filling the Dataset for Lookup


table

for JnlType")
Return False
Finally
If mySqlConnection.State = ConnectionState.Open Then
mySqlConnection.Close()
End If
End Try

My question is this: Is the Try Catch code smart enough to run the


Finally

statment after the MsgBox and then return false or do I need to write


the code some other way. This is what I want it to do but I am not sure I


have

it written correctly.





这篇关于尝试抓住问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆