try-catch-finally

Why does Java not support retrieval of exceptions from try/catch lost when an exception is thrown from finally?

不羁岁月 提交于 2019-12-12 11:19:57
问题 In Java 7, the feature was added to (via getSuppressed() ) get exceptions thrown from the implicit finally block of a try-with-resources statement. There still doesn't seem to be a way (that I know of) to do the opposite - when there is an explicit finally block and that throws an exception, masking the exceptions thrown and pending from the try/catch. Why does Java not provide functionality to get these buried/lost exceptions through a mechanism similar to getSuppressed() ? It would seem

Is finally block really necessary for the clean up code (like closing streams)?

徘徊边缘 提交于 2019-12-12 07:52:03
问题 I am very confused as to why do I need to need to put the clean-up code like closing streams in a finally block. I've read that the code in finally block will run no matter what (whether there's an exception); and after the finally block runs, the rest of the method continues. My question is: if the rest of the method has to continue then why don't I put the clean-up code after my try/catch block in a function? 回答1: The finally block will always run if an uncaught exception is thrown, but the

advantages and disadvantages of using try/catch [closed]

懵懂的女人 提交于 2019-12-12 04:59:33
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I want to ask what is the advantage and disadvantages of using try/catch ? and when I must use it and when I must not use it ? 回答1:

Avoiding an empty catch clause

一个人想着一个人 提交于 2019-12-11 22:32:59
问题 I have a function which checks to see if the user has made any changes, and if so, warns them of this fact. When they choose to discard their changes, I have another function which a) restores the state to pre-edit, and b) updates a global object which holds info about the current edit (including whether one exists). What I don't want to happen is that some error is thrown on trying to remove the edit box elements, and as a result the system doesn't update the global object flag or show the

try-catch-finally block in java

巧了我就是萌 提交于 2019-12-10 19:22:29
问题 As per my understanding, I want to follow the best practice for releasing the resources at the end to prevent any connection leaks. Here is my code in HelperClass. public static DynamoDB getDynamoDBConnection() { try { dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider())); } catch(AmazonServiceException ase) { //ase.printStackTrace(); slf4jLogger.error(ase.getMessage()); slf4jLogger.error(ase.getStackTrace()); slf4jLogger.error(ase); } catch (Exception e) {

System.exit() results unexecutable finally block [duplicate]

三世轮回 提交于 2019-12-10 13:46:40
问题 This question already has answers here : How does Java's System.exit() work with try/catch/finally blocks? [duplicate] (6 answers) Closed 6 years ago . I am working on My application's under maintanace module try { if (isUndermaintanace) { System.exit(1); } else { prepareResources(); } } catch (Exception e) { printStack(e); } finally { cleanResources(); } When I am passing isundermaintanace true finally not executing. What am I missing? Is there any other way to do that? 回答1: Finally 's don't

Return in the Finally Block… Why not?

老子叫甜甜 提交于 2019-12-09 14:09:32
问题 As MSDN mentions: The code in a Finally block runs after a Return statement in a Try or Catch block is encountered, but before that Return statement executes. In this situation, a Return statement in the Finally block executes before the initial Return statement. This gives a different return value. To prevent this potentially confusing situation, avoid using Return statements in Finally blocks. As I didn't understand a lot from this note, I'll take an example (VB.NET, I think in C# is the

Finally is not executed when in a Thread running in a Windows Service

拈花ヽ惹草 提交于 2019-12-09 08:08:58
问题 Can anyone explain why this finally block is not executed? I have read posts about when to expect finally block not be executed, but this seems to be another case. This code needs TopShelf and log4net. I am running .net 4.5 I guess it must be the Windows Service engine that kicks in on unhandled exceptions, but why is it running before the finally block has finished? using log4net; using log4net.Config; using System; using System.Threading; using Topshelf; namespace ConsoleApplication1 {

How does the try catch finally block work?

若如初见. 提交于 2019-12-09 05:04:19
问题 In C# , how does a try catch finally block work? So if there is an exception, I know that it will jump to the catch block and then jump to the finally block. But what if there is no error, the catch block wont get run, but does the finally block get run then? 回答1: Yes, the finally block gets run whether there is an exception or not. Try [ tryStatements ] [ Exit Try ] [ Catch [ exception [ As type ] ] [ When expression ] [ catchStatements ] [ Exit Try ] ] [ Catch ... ] [ Finally [

In a finally block, can I tell if an exception has been thrown [duplicate]

爱⌒轻易说出口 提交于 2019-12-06 16:32:00
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is it possible to detect if an exception occurred before I entered a finally block? I have a workflow method that does things, and throws an exception if an error occurred. I want to add reporting metrics to my workflow. In the finally block below, is there any way to tell if one of the methods in the try/catch block threw an exception ? I could add my own catch/throw code, but would prefer a cleaner solution as