try-catch-finally

Yield return from a try/catch block [duplicate]

帅比萌擦擦* 提交于 2019-12-19 02:37:08
问题 This question already has answers here : yield return with try catch, how can i solve it (10 answers) Closed 6 years ago . As Eric Lippert described in this article, yield return is not allowed within try/catch clauses. Is there a nice way I could get something like this, without having to write my own IEnumerator by hand: public IEnumerable<Data> GetData() { var transaction = Session.BeginTransaction()); try { IQuery q = CreateQuery(session); foreach (var result in q.Enumerable()) yield

Does code in finally get run after a return in Objective-C?

我的梦境 提交于 2019-12-18 07:41:52
问题 Consider the following code: @try { if (something.notvalid) { return; } // do something else } @catch (NSException *ex) { // handle exception } @finally { NSLog(@"finally!"); } If something is not valid and I return from within the try, does the code in @finally execute or not? I believe that it should but others I've spoken to don't think so and I'm unable to test this at the moment. 回答1: @finally code always executes according to here and here. A @finally block contains code that must be

@try - catch block in Objective-C

北城以北 提交于 2019-12-17 17:25:23
问题 Why doesn't @try block work? It crashed the app, but it was supposed to be caught by the @try block. NSString* test = [NSString stringWithString:@"ss"]; @try { [test characterAtIndex:6]; } @catch (NSException * e) { NSLog(@"Exception: %@", e); } @finally { NSLog(@"finally"); } 回答1: All work perfectly :) NSString *test = @"test"; unichar a; int index = 5; @try { a = [test characterAtIndex:index]; } @catch (NSException *exception) { NSLog(@"%@", exception.reason); NSLog(@"Char at index %d

Is there such case when in try\finally block the finally won't be executed?

不问归期 提交于 2019-12-17 16:56:06
问题 I'm studying for my test in Object Oriented Programming and I was wondering if there is any case what so ever that considering the following code: try { do something } catch (someException e) { } finally { do something } the finally block will not execute? 回答1: Yes. If you crash the Java VM or otherwise muck things up via native code, cause the program to terminate, or loop/wait infinitely inside the try block. Those are the only three cases which will avoid executing the finally block. 回答2:

Does return “happen after” finally?

这一生的挚爱 提交于 2019-12-17 16:01:57
问题 I am trying to convince myself that actions taken in the finally clause happen before the function return (in the memory consistency sense). From the JVM specification, it is clear that within a thread, program order is supposed to drive the happens before relationship -- if a happens b in the program order then a happens before b . However, I have not seen anything explicitly stating that finally happens before return, so does it? Or, is there some way that the compiler could reorder the

Why is try {…} finally {…} good; try {…} catch{} bad?

旧巷老猫 提交于 2019-12-17 05:33:30
问题 I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } catch // No args, so it will catch any exception {} reader.Close(); However, this is considered good form: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } finally // Will execute despite any exception { reader.Close(); } As far as I can tell, the only difference between

Why is try {…} finally {…} good; try {…} catch{} bad?

ⅰ亾dé卋堺 提交于 2019-12-17 05:33:19
问题 I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } catch // No args, so it will catch any exception {} reader.Close(); However, this is considered good form: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } finally // Will execute despite any exception { reader.Close(); } As far as I can tell, the only difference between

What happens if a finally block throws an exception?

一世执手 提交于 2019-12-17 04:11:19
问题 If a finally block throws an exception, what exactly happens? Specifically, what happens if the exception is thrown midway through a finally block. Do the rest of statements (after) in this block get invoked? I am aware that exceptions will propagate upwards. 回答1: If a finally block throws an exception what exactly happens ? That exception propagates out and up, and will (can) be handled at a higher level. Your finally block will not be completed beyond the point where the exception is thrown

in C# is it possible to force control to pass through a finally block if an exception is thrown by an associated catch block?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 07:10:14
问题 I know that in Java, if an exception is caught by a catch clause and its catch block throws an exception, control would pass throw the associated finally block (if any) before the thread is terminated. This does not appear to be the case in C#, however. It is possible to almost mirror this behavior in C# is by putting a try-finally statement inside the try block of the try-catch statement with the catch block that throws the exception, but that would be a problem if, for example, the finally

Is it ever good pratice to throw an exception in a finally block?

旧巷老猫 提交于 2019-12-12 18:52:28
问题 There's a good question Catch block is not being evaluated when exceptions are thrown from finallys that is discussing some of the sometimes unexpected results of throwing an exception in a finally block. I can't think of any good reason why you would want to throw an exception in a finally block. If there was a previous exception, it would always be lost. I've always seen finally used to clean up in ways that should never throw an exception. Can anyone explain when it would be appropriate to