抛出异常时方法(函数)是否返回值? [英] Does a method (function) return a value when an exception is thrown?

查看:676
本文介绍了抛出异常时方法(函数)是否返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我检查了一个正在找到的索引,并假设,如果找不到索引,则抛出一个异常,方法或函数会返回一些东西吗?在VB.NET代码中,这将是:



Suppose I check on an index being found and suppose, if the index is not found I throw an exception, will the method or function return something? In VB.NET code this would be:

Function IdLikeToKnow() As Boolean
	Dim indexFound As Boolean = False
	If indexFound Then
		Return True
	Else
		Throw New IndexOutOfRangeException
		'Is the below statement evaluated??????
		Return False
	End If
End Function





有谁有想法?这不是一个非常紧迫的问题,但我想知道......

提前问候并感谢...



Anybody has an idea? It is not a very urgent question, but I would like to know...
Regards and thanks in advance...

推荐答案

到目前为止,你没有得到满意的答案。你到目前为止所得到的只会误导你。



简而言之,不仅不会返回任何东西,而且如果发生异常,你不需要返回任何东西。在您的情况下可能会或可能不会使用抛出异常,但在本地捕获它将是一个最糟糕的建议。说,你有两个可能的结果:真或假;然后你应该总是返回一个或另一个。但是你可能需要3个结果:true,false和用这些条件调用这个函数毫无意义。然后,在最后一种情况下,您可以抛出异常并在堆栈的某处捕获异常,然后您将获得效果,就好像甚至没有调用该函数一样。结构化异常机制类似于时间机器。



也许我过去的答案可以帮助你:

C#构造函数中的异常导致调用者分配失败? [ ^ ],

在操作系统中存储.net例外的地方 [ ^ ]。



-SA
So far, you did not get a satisfactory answer. What you got so far can only mislead you.

In brief, not only nothing will be returned, but you don't need to return anything in case of exception. Throwing exception may or may not be used in your case, but catching it locally would be a worst advice. Say, you have two possible results: true or false; then you should always return one or another. But you may need 3 results: true, false and "calling this function with these conditions makes no sense". Then, in last case, you can throw an exception and catch exceptions somewhere up the stack, then you will get the effect as if the function was not even called. Structured exception mechanism is similar to "time machine".

Perhaps my past answers can help you:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^].

—SA


这段代码很难看。如果找不到索引,你的应用程序将崩溃。

除非你在调用这个函数的代码中处理错误,否则将不会返回任何内容并且应用程序将崩溃。



事实 - 即使你在父方法中处理错误,你仍然不会得到任何返回的东西(不是从这种方法)。



要返回一些东西,请在此方法中使用 try-catch
This code is ugly. If index is not found your application will crash.
There will be nothing returned and the application will crash unless you handle the error in the code that calls this function.

Fact of matter - Even if you do handle the error in the parent method, you will still not get anything returned (not from this method).

To return something use try-catch within this method.


当有一个throw语句时,它不会出现循环并检查任何catch块,我不会在抛出后执行语句,



catch块中的语句将被执行



尝试

{

抛出新bla bla;

抛出后行 - 这不会被执行< br $>
}

catch

{

//其中的statemens将被执行

}





[ ^ ]
Nope when there is a throw statement , it gets out of the loop and checks for any catch block, i wont execute the statements after throw,

The statements inside the catch block will be executed

Try
{
throw new bla bla;
line after throw-- this ll not be executed
}
catch
{
// the statemens inside this will be executed
}


[^]


这篇关于抛出异常时方法(函数)是否返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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