最后尝试Catch Else [英] Try Catch Else Finally

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

问题描述

我的另一个愿望。我希望在Try Catch结构中有一种方法

来说出是否有错误做某事。像其他人一样

声明。最后尝试Catch Else。


另外因为我理解终于运行是否发现错误或

没有,我还没有找到最终用途。

解决方案

这就是尝试的目的。做假设有没有错误的b / b
。问题是处理任何错误。无论是否有错误,最终都要运行




" cj" < cj@nospam.nospam>在消息中写道

新闻:uO ************** @ TK2MSFTNGP14.phx.gbl ...

我的另一个愿望。我希望在Try Catch结构中有一种方法可以说出是否有错误做某事。就像其他声明一样。
最后再尝试抓住它。

也因为我理解终于运行是否发现了错误,
我终于找不到用途了然而。





我不太清楚我是否理解这个问题。


Try块的主线是隐含的''如果没有错误就这样做''

分支,只有在发生异常时才会发生捕获。


你有一个你想看的功能的例子。

至于最后


一个例子是关闭文件


eg


Dim fs As FileStream

Dim reader As TextReader


试试


fs =新FileStream(" test.txt",FileMode.Open)

reader = New StreamReader(fs)

>
虽然真实


Dim str As String = reader.ReadLine

If(str Is Nothing)Then

Ë xit同时

结束如果


Trace.WriteLine(str)


结束时


最后


如果不是fs则没有那么

fs.Close()

结束如果


结束尝试


在阅读过程中说出错(或者如果我们做某事

更复杂的是输入而不仅仅是将其转储到跟踪中)然后当我们发生异常时我们希望关闭文​​件流。我们

也希望它在正常流量期间关闭。

hth,

Alan


你是说如果我有,如果第2行投掷和例外它将

跳过3,4和5然后直接赶上?


如果是这样,接下来的问题是我怎么知道第2行抛出了

异常而不是第1或第4行?


试试

1

2

3

4

5 br />
catch

a

结束尝试


Marina Levit [MVP]写道:


" cj" < cj@nospam.nospam>在消息中写道
新闻:uO ************** @ TK2MSFTNGP14.phx.gbl ...

我的另一个愿望。我希望在Try Catch结构中有一种方法可以说出是否有错误做某事。就像其他声明一样。
最后再尝试抓住它。

也因为我理解终于运行是否发现了错误,
我终于找不到用途了然而。




Another wish of mine. I wish there was a way in the Try Catch structure
to say if there wasn''t an error to do something. Like an else
statement. Try Catch Else Finally.

Also because I understand Finally runs whether an error was caught or
not, I haven''t found a use for finally yet.

解决方案

That''s what the Try is for. To do stuff with the assumption that there are
no errors. The catch is to deal with any errors. And the finally is to run
regardless of whether or not there were errors.

"cj" <cj@nospam.nospam> wrote in message
news:uO**************@TK2MSFTNGP14.phx.gbl...

Another wish of mine. I wish there was a way in the Try Catch structure
to say if there wasn''t an error to do something. Like an else statement.
Try Catch Else Finally.

Also because I understand Finally runs whether an error was caught or not,
I haven''t found a use for finally yet.




Not too sure if I understand the question.

The mainline of the Try block is the implied ''do this if no error''
branch, with the the catch occuring only if an exception occurs.

Do you have an example of the functionality that you would like to see.
As for the finally

One example is to close files

e.g.

Dim fs As FileStream
Dim reader As TextReader

Try

fs = New FileStream("test.txt", FileMode.Open)
reader = New StreamReader(fs)

While True

Dim str As String = reader.ReadLine
If (str Is Nothing) Then
Exit While
End If

Trace.WriteLine(str)

End While

Finally

If Not fs Is Nothing Then
fs.Close()
End If

End Try

Say something went wrong during a read (or if we we doing something
more complicated with the input than just dumping it to trace) then
when we the exception occurs we want the filestream to be closed. We
also want it to be closed during the normal flow.
hth,
Alan


So are you saying if I have that if line 2 throws and exception it will
skip 3, 4 and 5 and go straight to catch?

If so, the next question is how do I know it was line 2 that threw the
exception and not line 1 or 4?

try
1
2
3
4
5
catch
a
end try

Marina Levit [MVP] wrote:

That''s what the Try is for. To do stuff with the assumption that there are
no errors. The catch is to deal with any errors. And the finally is to run
regardless of whether or not there were errors.

"cj" <cj@nospam.nospam> wrote in message
news:uO**************@TK2MSFTNGP14.phx.gbl...

Another wish of mine. I wish there was a way in the Try Catch structure
to say if there wasn''t an error to do something. Like an else statement.
Try Catch Else Finally.

Also because I understand Finally runs whether an error was caught or not,
I haven''t found a use for finally yet.




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

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