Idea for doing almost same work in both catch & finally(C#3.0) - c#-3.0

I have a requirement.
I am processing some files and after the processing are done I am archiving those files into an archive folder with timestamp appended.
The file archiving and putting time stamp portion I am doing in the Finally block. Now a new requirement has come where I need to mail if something wrong goes in the original files and then I need to archive the same. Now this piece of code I need to handle in the catch block.
But if I write the code entirely in the catch block, then it will fire only if there is an exception; otherwise not.
So basically I am writing the same pice of code in both the catch and finally block.
What is the standard and recommended approach you people think will be better in this case?
I am using C#3.0
Thanks.

A finally block is really for code that "always" has to run, even after an exception. This typically means cleanup type code. It's a "code smell" to see processing type code in a finally block.
Maybe you can post your code, and we can suggest a better way for you.

Related

Can a matlab function called within a script cause the script to break?

I am running a script which calls a function, and if a certain condition is met, inside the function, I want the whole thing just to terminate (and by that I do not mean I want to close matlab using exit). Is that possible? I know I can use return or break to return to the script, however I want the script to stop as well if this condition is met.
The only function I know of that does this is error. This throws an exception, and, if no exception handlers with try and catch are installed in the calling script, will terminate and return to command prompt. Which is what you want, as far as I understand. It prints an error message though. This could be suppressed if you guard all code in the top-level script with a try catch handler. However this will have to be specific to the one error and it makes debugging ("stop-on-error") much more difficult.
The thing is that the only use case I see for this behavior (termination of whole program on certain event) is when a non recoverable error occurs, and in that case printing an error message is indeed appropriate.
In case the script is successful termination of the whole program is not really the right way. All functions should return to give the upper layers of the code to perform some clean-up action, like saving the output data or so.

Download stops due to missing data matlab

I am using websave function of MATLAB to download data from a website. The data is arranged by date. I have put the date in a loop to run the code for a certain period of time. However, on some dates, there is no data available. For those dates, the program halts and give 'internal error'.
I want to know how can I download data without interruptions even if there is missing data on some dates avoiding this internal error.
First of all, that sounds like a very poorly designed API if you're getting internal errors for an empty query result. Maybe there is a better way to make the call that you need? If you show some more code maybe we can help you.
That being said, you can use the try/catch construct to surround the call to websave, that way if it throws an error, it will not interrupt the flow of your program.
for k = date_range
try
websave(filename, url);
catch
% There was some error
disp('Skipping this one!')
end
end

How is try catch evaluated in matlab?

I wonder how a try-catch block is evaluated in matlab. In particular, is the try-catch block evaluated in runtime or "compile time"?
Also, is a try-catch block expensive?
If someone have a link to any documentation that would be much appreciated.
(Btw, I know that try-catch is not the best solution in most cases. Still I would like to know how it works, since I have used it in some code).
try and catch blocks allow you to override the default error behavior
for a set of program statements. If any statement in a try block
generates an error, program control goes immediately to the catch
block, which contains your error handling statements.
To learn more about try and catch in Matlab:Try and Catch
Try and catch block is always executed in run-time and is used to catch the errors that occur and has error-handling statements. So if your code generates some error and you want to handle it, use it. Using error-handling is good programming practice..

Errordlg for any given Matlab error

I have a question about inner Matlab error management. Right now I have quite a large program with a lot of variables and functions that cumulated over my code writting and I'm 100 percent sure that I did not catch all the bugs and mistakes in the program and I don't want it to crash completely when is used by layman user. So, is there a way to display errordlg message and for example restart the program when there will be any given error directly by Matlab (for example when I forgot to declare a global variable etc.)?
Thanks for answers, Peter
Crashes are good, because they force users to report bugs.
If you don't want to go that route, Matlab provides try-catch: wrap your code in a try-catch block. If there is an error, you'll find yourself in the catch block where you can have Matlab send you an email with the error message, and restart the program if necessary.
You can use try/catch statements to respond to errors in your program. There's more information here.

Can I have business logic in Finally block?

Is it advisable to have business logic in a finally block?
I have to send an email notification when a job is over (whether successful or not). Can I place the email logic in finally block?
The main danger I can think of is that the finally block has the ability to silently swallow exceptions and return values from the try block itself.
For example,
try {
doSomethingFancy();
} finally {
sendEmail();
}
If doSomethingFancy throws an exception, you will attempt to send an email. If somehow sending an email fails, sendEmail could throw an exception. This exception will "override" the original thrown one, and you'll never see it. It'll just vanish.
You could code around this defensively with more try / catch blocks, but just be aware...
Ideally you should have your business logic in Try block and Finally block should contain any cleanup task or any thing that must happen irrespective of success or failure of try block . You also need to make sure that the code in finally block does not cause any exception otherwise as Steven mentioned, the original exception will be lost if any.
You could do that in catch block in case if you intend to send error condition to designated email id. finally block is generally used for graceful-release of the resources mostly. I do not recommend sending email or performing any business rules within the finally block.
In my mind,
try {
doSomethingFancy();
catch(Exception ex) {
logError(ex);
}
sendMail();
Is the perfect pattern for this. Finally block should only be used to clean the mess the code in the try block could have left behind.