putting heavy code in finally block - try-catch-finally

I am adding a new code in my application , and this code saves some data in DB.
Now, this code is after a try, catch block. If the exception occurs, the catch block returns the exception. So in that case, my code(after the catch block) will not be executed (to save data in DB).
I know that i can use finally block in this case to make sure that my code to save data is always executed irrespective of whether the exception occurs.
But i am thinking, is it a good practice to have code that does some heavy work in finally block.
If it isn't a good practice, what approach can i follow?

Related

Flink Streaming JdbcSink Exception Handling

I am using JdbcSink to insert processed events into a Postgres DB.
Occasionally, I receive bad records from the source stream, and it fails to insert into the database (java.sql.BatchUpdateException) since it fails to satisfy some table constraints.
I can obviously pass the events through a Flink filter operator to filter them, but the filter would then become a complex code to check every possible combination of failure. Instead of a filter, I would like to catch the BatchUpdateException thrown by the JdbcSink, log it and continue processing other events.
No luck trying to find a way to catch BatchUpdateException from JdbcSink.
Has someone tried doing similar with success?
I took a quick look at the code, and didn't see an obvious solution. You could extend the JdbcOutputFormat class and override the attemptFlush method, and then clone the JdbcSink class and modify your version to use your output format class.

How to trigger the execution of the compensation flow for the activities used within an Automatonymous state machine?

My activities throw exceptions from time to time during the execution, so I've implemented the Faulted methods of Activity<TInstance> to handle that, discarding the changes made in the Execute method. I thought that there's some wiring underneath in Automatonymous that makes it so that the Faulted method executes when the Execute method throws an exception and then calls the Faulted methods for the activities that were executed already. It turns out that there's no such thing, as my Faulted methods are never executed.
Should I call those myself in a try/catch block instead? I could produce the BehaviorExceptionContextProxy based on BehaviorContext and the exception thrown. The only next Behavior I could pass would be the one inserted into that Activity's Execute method, but logically that means I'm compensating in the wrong direction as that next Behavior is actually to be executed after this one succeeds, so I'd compensate too much.
I also tried to use the Catch in the state machine, which does handle the exception, however, I couldn't find any way to start the execution of the compensation flow for the activity that failed when I only have the ExceptionActivityBinder present.
Is there any good way to trigger the compensation flow of the activities?
An activity within a state machine (using Automatonymous) is much different than an activity within Courier. Unfortunately, they both have the same name, which can create confusion.
When an activity throws an exception, the Faulted method of the next activity in the behavior is called. If that method is a regular activity method (such as .Then, .Publish, etc.) it is skipped, since the Faulted method of those activities just calls the next activity in the behavior.
A Catch activity, however, can be used to catch the exception and execute a rescue behavior (which is a sequence of activities).
Either way, the Faulted method of the activity which throws an exception within the Execute method is not called. So yes, you should use a try/catch, but allow the exception to flow back out of the Execute method so that the behavior handles it properly.

Swift 2 How do I force Session data task to execute .resume() before continuing

I'm going to crazy trying to figure out how to prevent the rest of my code from executing before a session dataTaskWithRequest is finished. No matter what I do, the data task won't .resume() until everything else finishes. Nothing works. Not using a a while loop. Not putting the task inside a separate thread (dispatch_async). I'm at my wits end. To make matters worse, I feel like I'm overlooking something simple because it seems nobody else is asking this question.
NSURLSession is an asynchronous loading system. When you tell it to start, it begins fetching data in its own thread, and your code continues to run. If you want to have code that executes after the request returns data, the easiest way is to put that code in a completion handler block (by calling dataTaskWithRequest:completionHandler:).

Promises without `.then`

Is there any downside to using a promise with only .catch section, but without .then at all?
I'm asking about the cases where the resolution result is not needed, only error handling.
Is this a good pattern to rely on .catch only and skip .then?
Or is it something that depends on which promise implementation it is?
Conceptually, there's nothing wrong with an operation that only has an error handler and has nothing else to do upon successful completion. If that's all it needs, then that's fine. For example, suppose you're updating a server with some new data from the client. If the data is successfully sent to the server, there's nothing else to do because the operation is complete, but if there's an error, then there is perhaps something else to do (retry, inform the user, correct the data based on the error code, etc...).
To comment on whether that's the right way to design your specific code, we'd have to see the actual code and understand what it is doing and then form an opinion on whether that is the best way to structure that specific code.
If I was designing a general purpose function, I'd certainly provide both completion (resolving the promise) and error (rejecting the promise) so the caller could hook into either one. But it is really up to the caller which events they want to know about and if only the error matters, then just having a .catch() is fine.

Error on reassigning workflows in FileNet

I am currently developing some Javacode that reassigns workflows that belong to a case in a FileNet 5.0 system.
In some cases I get the following error in the logs:
[FNRPE2131090209E]WorkObject Operation is not valid in this context.
WorkObjectOperation is only valid when an instruction sheet interpreter update function is called. That happens when a WobUpdateUnlocked occurs. In any other case, this exception is thrown. for
If I google the error I only find a few hits that specify this error but it does not give a clear reason to what is the situation for this error.
It is thrown when I execute the vwWorkObject.doReassign() call.
Does someone know what the state is that is causing this error.
as it turns out this exception means that your workflow is in either a waitfor or delay condition before comming to an assign step.
This took me a while to figure out, but in my case it means that I can just ignore this situation, as there is no need for a reassign as the system is going to do it for me as soon as the workflow reaches the assign step.

Resources