using(...)语句是try {} finally {}的语法糖.
但是,如果我有一个如下所示的使用声明:
using (FileStream fs = File.Open(path))
{
}
Run Code Online (Sandbox Code Playgroud)
现在我想要捕获打开此文件可能导致的异常(这是相当高风险的代码,因为它可能因环境而失败),但是如果我在里面写try-catch会不会重复?当代码被编译为IL时,我假设当代码被JIT时,重复将被删除?
但是,我想要捕获打开文件可能导致的异常(所以我应该将try-catch包装在using语句的作用域之外),以及我在using块中做的任何异常,所以我应该添加try-catch在街区内.
这似乎是我在CLR可能在里面做了很多重复.CLR是否添加了catch子句?
我的同事认为使用声明是混乱的(但这是因为由于我需要对它们进行硬编码,所以单行很长,因为我需要非常快速地更改它们并且无法访问代码库的其他部分).说同事不使用using语句,但是using语句和try-finally/try-catch-finally之间是否存在任何功能差异?我确实看到了一个这样的案例,其中WCF服务有一个关于使用finally和返回值的一个鲜为人知的角落案例(最后的事情).解决方案是使用检查块.在C#中有这样的东西吗?
另一方面,是否所有类型都实现了非托管资源的IDisposale所有者?与我的朋友的讨论指出了不是的答案.(我也在本论坛的使用部分阅读了一些主题,其中有一些非常好的知识).
请考虑以下代码:
@try {
if (something.notvalid)
{
return;
}
// do something else
} @catch (NSException *ex) {
// handle exception
} @finally {
NSLog(@"finally!");
}
Run Code Online (Sandbox Code Playgroud)
如果something无效并且我从try中返回,代码是否@finally执行?我相信它应该,但我已经说过的其他人不这么认为,我现在无法测试这一点.
我已经使用try-catch多年了,但我从来没有学过如何以及何时使用finally,因为我从未明白finally(我读过坏书)的意思?
我想问你finally在我的案子中的用法.
我的代码示例应该解释一切:
$s = "";
$c = MyClassForFileHandling::getInstance();
try
{
$s = $c->get_file_content($path);
}
catch FileNotFoundExeption
{
$c->create_file($path, "text for new file");
}
finally
{
$s = $c->get_file_content($path);
}
Run Code Online (Sandbox Code Playgroud)
终于正确使用了吗?
更准确的问题:
我可以使用finally(在未来的PHP版本或其他语言中)处理"创建一些不存在的东西"操作吗?
说我有这样的代码:
try:
try:
raise Exception("in the try")
finally:
raise Exception("in the finally")
except Exception, e:
print "try block failed: %s" % (e,)
Run Code Online (Sandbox Code Playgroud)
输出是:
try block failed: in the finally
Run Code Online (Sandbox Code Playgroud)
从print语句的角度来看,有没有办法访问try中引发的异常,还是它永远消失了?
注意:我没有考虑用例; 这只是好奇心.
python exception-handling exception try-catch try-catch-finally
我正在攻读面向对象编程的测试,我想知道是否有任何案例,考虑到以下代码:
try {
do something
} catch (someException e) {
} finally {
do something
}
Run Code Online (Sandbox Code Playgroud)
该finally块不会执行?
目前我正在进行代码优化,我正在使用try .. finally块来尊重我的对象.
但我感到困惑的是,当我在finally块中创建对象的空引用时,如何管理返回对象.??
在try块中返回一个对象时,它会在编译期间创建一个预编译的语句吗?或者在返回语句时在堆中创建新引用?或者只返回对象的当前引用?
以下是我的研究代码.
public class testingFinally{
public static String getMessage(){
String str = "";
try{
str = "Hello world";
System.out.println("Inside Try Block");
System.out.println("Hash code of str : "+str.hashCode());
return str;
}
finally {
System.out.println("in finally block before");
str = null;
System.out.println("in finally block after");
}
}
public static void main(String a[]){
String message = getMessage();
System.out.println("Message : "+message);
System.out.println("Hash code of message : "+message.hashCode());
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
在最后块中
的str:-832992604
的最后块中的try块阻塞哈希代码 消息:Hello world 哈希消息代码:-832992604
当我看到返回对象和调用对象具有相同的哈希码时,我感到非常惊讶.所以我对对象引用感到困惑.
请帮我清楚这个根本.
我在.net 3.5中创建asp.net web应用程序,我想知道何时使用以及何时不使用Try Catch Finally块?特别是,我的大部分try catch都围绕执行存储过程并填充文本字段或网格视图?当您执行存储过程并填充数据显示控件时,您会使用Try Catch EVERYTIME吗?
我的代码块通常如下所示:
protected void AddNewRecord()
{
try
{
//execute stored proc
// populate grid view controls or textboxes
}
catch (Exception ex)
{
//display a messagebox to user that an error has occured
//return
}
finally
{ }
}
Run Code Online (Sandbox Code Playgroud) 当子句调用时,A RuntimeException被抛出try而没有被捕获.finallySystem.exit()
public static void main(String[] args) {
try {
Integer.valueOf("NotANumber");
} finally {
System.out.println("finally");
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是
finally
Run Code Online (Sandbox Code Playgroud)
如果System.exit(0)从finally中删除,则输出为
finally
Exception in thread "main" java.lang.NumberFormatException: For input string: "NotANumber"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.valueOf(Integer.java:554)
at exception.MyExcepTest.main(MyExcepTest.java:20)
Run Code Online (Sandbox Code Playgroud)
当"终于"可能出现之前,之后或在的meesage之间NumberFormatException.
任何人都可以解释一下吗?
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = null;
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
} finally {
fr.close();
}
Run Code Online (Sandbox Code Playgroud)
将fr.close()显示一个错误:
fr无法解决
我曾经读过在finally块中关闭一个文件是一个很好的做法.
那是做错了什么的?
这就是我想要做的:
try {
//code
} catch (Exception e) {
return false;
} finally {
//close resources
}
Run Code Online (Sandbox Code Playgroud)
这会有用吗?这是不好的做法吗?这样做会更好:
boolean inserted = true;
try {
//code
} catch (Exception e) {
inserted = false;
} finally {
//close resources
}
return inserted;
Run Code Online (Sandbox Code Playgroud)