考虑这两个例子
<?php
function throw_exception() {
// Arbitrary code here
throw new Exception('Hello, Joe!');
}
function some_code() {
// Arbitrary code here
}
try {
throw_exception();
} catch (Exception $e) {
echo $e->getMessage();
}
some_code();
// More arbitrary code
?>
Run Code Online (Sandbox Code Playgroud)
和
<?php
function throw_exception() {
// Arbitrary code here
throw new Exception('Hello, Joe!');
}
function some_code() {
// Arbitrary code here
}
try {
throw_exception();
} catch (Exception $e) {
echo $e->getMessage();
} finally {
some_code();
}
// More arbitrary code …Run Code Online (Sandbox Code Playgroud) 我知道如何尝试,捕获和最终工作(大多数情况下),但我有一件事我想知道:在try-catch-finally之后返回语句会发生什么,而我们已经在try(或者catch)中返回了)?
例如:
public boolean someMethod(){
boolean finished = false;
try{
// do something
return true;
}
catch(someException e){
// do something
}
finally{
// do something
}
return finished;
}
Run Code Online (Sandbox Code Playgroud)
假设在尝试中没有出错,所以我们返回true.然后我们将进入最后我们做的事情,比如关闭连接,然后呢?
在我们在finally中做了一些东西之后,方法会停止吗(所以方法在try中返回true),或者方法会在finally之后继续,导致返回完成(这是假的)?
在此先感谢您的回复.
正如MSDN 提到的那样:
Finally块中的代码在遇到Try或Catch块中的Return语句之后但在执行Return语句之前运行.在这种情况下,Finally块中的Return语句在初始Return语句之前执行.这给出了不同的返回值.要防止这种可能令人困惑的情况,请避免在Finally块中使用Return语句.
由于我从这个笔记中不了解很多,我将举一个例子(VB.NET,我认为在C#中情况类似):
Try
HugeOp()
Return "OK"
Catch
Return "NOK"
Finally
Return "Finally"
End Try
Run Code Online (Sandbox Code Playgroud)
现在,为什么在C#和VB.NET中这都是非法的?
我尝试在Swift2中使用错误处理建模.
do {
try NSFileManager.defaultManager().removeItemAtPath("path")
} catch {
// ...
} finally {
// compiler error.
}
Run Code Online (Sandbox Code Playgroud)
但似乎那里没有finally关键字.如何try-catch-finally pattern在Swift中实现.欢迎提供任何帮助.
它看起来像是根据一些初步测试,但我想知道的是,如果它保证返回或在某些情况下它不能返回?这对我的应用程序至关重要,但我还没有找到一个用例,但它不会返回.
我想获得有关该主题的专业知识.
所以,我已经在PHP在线手册上阅读了今天的例外情况,并意识到我还没有理解finally关键字的目的或真正的必要性.我在这里看过一些帖子,所以我的问题略有不同.
我知道我们最终可以这样使用:
function hi(){
return 'Hi';
}
try {
throw new LogicException("Throw logic \n");
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
}
echo hi();
Run Code Online (Sandbox Code Playgroud)
输出:
Fatal error: Uncaught LogicException: Throw Logic in C:\Users\...a.php:167
Stack trace:
#0 {main}
thrown in C:\Users\...a.php on line 167
Run Code Online (Sandbox Code Playgroud)
所以,在这种情况下函数hi(); 没有被执行,并且有充分的理由.我理解如果没有处理异常,php解释器会暂停脚本.好.到目前为止,我读到的内容,最终使我们能够执行函数hi(); 即使没有处理异常(即使我不知道为什么)
所以,这个我明白了.
try {
throw new LogicException("Throw logic \n");
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
}finally{
echo hi();
}
Run Code Online (Sandbox Code Playgroud)
输出:
Hi
Fatal error: Uncaught LogicException: Throw Logic in C:\Users\...a.php:167
Stack trace:
#0 {main}
thrown …Run Code Online (Sandbox Code Playgroud) 如果你运行下面的代码,它实际上在每次调用goto后执行finally:
int i = 0;
Found:
i++;
try
{
throw new Exception();
}
catch (Exception)
{
goto Found;
}
finally
{
Console.Write("{0}\t", i);
}
Run Code Online (Sandbox Code Playgroud)
为什么?
try {
if (isFileDownloaded)
//do stuff
else
throw new CustomException()
}
catch (Exception e)
{
// something went wrong save error to log
}
finally
{
//release resources
}
Run Code Online (Sandbox Code Playgroud)
我的问题是catch抓住ApplicationExceptiontry块中抛出的东西吗?是编码风格差吗?它应该用另一种方式写吗?
我正在学习在线java课程,使用Java编程简介.
在I/O章节中,下面的代码引入了以下语句:
顺便说一句,在本程序结束时,您将在try语句中找到我们的第一个有用的finally子句示例.当计算机执行try语句时,无论如何都保证finally子句中的命令被执行.
该程序位于第11.2.1节的末尾,是一个简单的程序,只读取文件中的一些数字并以相反的顺序写入.
main方法中的相关代码是(data是Reader,result是Writer):
try {
// Read numbers from the input file, adding them to the ArrayList.
while ( data.eof() == false ) { // Read until end-of-file.
double inputNumber = data.getlnDouble();
numbers.add( inputNumber );
}
// Output the numbers in reverse order.
for (int i = numbers.size()-1; i >= 0; i--)
result.println(numbers.get(i));
System.out.println("Done!");
} catch (IOException e) {
// Some problem reading the data from the input file.
System.out.println("Input Error: " + e.getMessage()); …Run Code Online (Sandbox Code Playgroud) 任何人都可以解释为什么这个finally块没有被执行?我已经阅读了关于何时期望最终阻止不被执行的帖子,但这似乎是另一种情况.此代码需要TopShelf和log4net.我正在运行.net 4.5
我想它必须是Windows服务引擎启动未处理的异常,但为什么它在finally块完成之前运行?
using log4net;
using log4net.Config;
using System;
using System.Threading;
using Topshelf;
namespace ConsoleApplication1
{
public class HostMain
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.Service<HostMain>(s =>
{
s.ConstructUsing(name => new HostMain());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetServiceName("TimerTest");
});
}
public void Stop()
{
LogManager.GetLogger("MyLog").Info("stopping");
}
public void Start()
{
XmlConfigurator.Configure();
LogManager.GetLogger("MyLog").Info("starting");
new Thread(StartServiceCode).Start();
}
public void StartServiceCode()
{
try
{
LogManager.GetLogger("MyLog").Info("throwing");
throw new ApplicationException();
}
finally
{
LogManager.GetLogger("MyLog").Info("finally");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出 …