|
Thanks a lot. This solves my problem!
|
|
|
|
|
Hi
I have a rather simple question. I have quite a few try/catch blocks in my system, but I don't really need to handle the exceptions catched. So I just use something like this:
try
{
}
catch (Exception error)
{
}
What I want to know is, since I'm not using "error" that I'm declaring, can I just have a parameterless catch like the following:
try
{
}
catch
{
}
|
|
|
|
|
You can.
But why are you doing it? It's a terrible practice to use exceptions as part of normal program flow. Exceptions are supposed to be *exceptional* - and there are extremely few cases where it would make any sense to just catch any exception, ignore it, and attempt to continue.
|
|
|
|
|
What is the sense in catching an exception and then ignoring it?
|
|
|
|
|
To prevent my system from crashing when an error does occur.
|
|
|
|
|
But once the error has occured, don't you want user to know? Or will your application behave normally even after any kind of exception occurs in it?
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Presumably your program is supposed to *do* something, not merely "not crash". All that can be achieved by catching exceptions and doing nothing about them is to prevent the program from exiting because of the error. But there's a reason why programs exit by default when errors occur: The program state is no longer well known. Programs close by default in order to prevent a program from *seeming* to work but actually producing corrupt data.
If you are getting exceptions a lot of the time without any associated external reason (hardware failure, network failure, out of disk space/memory, and so on) it just means your code isn't correct, and you should take the time to find out what's wrong with it and correct it.
|
|
|
|
|
Etienne_123 wrote: To prevent my system from crashing when an error does occur
Most often that does not make sense at all.
An exception indicates something went wrong, you can not just ignore it, you should:
1) probably log it
2) act on it (if nothing else, throw it again, so a higher level of the code can act on it).
3) consider informing the user.
And you should only catch the exceptions you can handle in a meaningful way, hence catch them as specific as possible.
Examples:
- when writing to a file, if it fails (error in path, disk full, whatever IOException) you can't ignore that, since then your output would be uncertain; here you must inform the user, unless you have a retry that succeeds.
- when parsing user input, a parse exception has to be reported to the user so he can re-enter data.
The rare exception to the rule is e.g. where you have a retry loop, then all exceptions except for the one in the last retry iteration can be left unhandled and unreported (I suggest to include a comment as to why a catch block is empty then); the exception in the last retry iteration has to be handled though, since that is the exit result of the retry loop.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
I think you need to study exceptions: what they are for and what action you should take when you catch one.
|
|
|
|
|
"On Error Resume Next", and it should be taken out and shot.
I are Troll
|
|
|
|
|
That one belongs exclusive to VB language
|
|
|
|
|
That exact statement, yes, that's confined to Basic, but the pattern can be found in all languages.
I are Troll
|
|
|
|
|
I use parameterless catches on occasion, such as:
try
{
Utilities.DoSQLNonQuery(cmd);
}
catch (Exception) { }
Which is part of my fault logging system. If I can't log a fault, I can't use the exception, but I don't want to lose the user data either.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
"Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
|
|
|
|
|
that is all right, as long as you leave the comment there.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Yes, you can. But the whole point of catching exceptions is to identify an 'exceptional' situation in your code and handle it properly. Having empty catch blocks is worser than not handling exceptions in your code. As a temporary measure, you can atleast log the error and show a message to the user that something wrong has happened.
|
|
|
|
|
Why would you ask us this instead of just trying it ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Christian Graus wrote: Why would you ask us this instead of just trying it ?
Come on Christian, you know the answer as well as the rest of us.
|
|
|
|
|
I did try it and it worked. I was merely making sure.
Why would you reply to a message when you don't aleast try and give an answer? This is the second time you've replied like this to one of my questions. I've said it before and I'll say it again, these forums aren't JUST for experts.
I didn't say I'm not going to handle my exceptions, I just wanted to know whether an empty and parameterless catch will work fine for the time being, as I still need to establish what my user wants to have done when an exception occurs.
|
|
|
|
|
Etienne_123 wrote: I still need to establish what my user wants to have done when an exception occurs.
This is not the way to look at it. If an exception occurs then it indicates a fault in your program, so you need to discover what the fault is and whether the program should continue. There are very few occasions when you should allow the program to continue, as your program, and thus your data/files/security may now be compromised. From this point on anything can happen.
|
|
|
|
|
Etienne_123 wrote: these forums aren't JUST for experts
correct.
everyone is welcome with some eagerness to learn, which includes reading documentation, using Google, experimenting a bit, and asking questions when nothing else helps or some advice is required.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
I'm trying to send 8-bits of output data in C# from my netbook via a USB to Parallel cable. I already have inpout32.dll and thought I could use the integer output address 888 but this doesnt seem to be working?
Can anyone help me with which address I should be using or have code which can send data via a USB to Parallel cable?
Thanks for any help
|
|
|
|
|
Hi I am not sure whether this is your problem,
There is a general problem with USB to parallel. I know for sure that many times USB to parallel does not work and "true" parallel port is required.
T o answer that, Is there another program or application that can do the same thing via the same port and succeed ?
|
|
|
|
|
Don't know about the parallel cable bit, but it may be worth checking out #USBLib[^]. I understand it is good for handling USB communication.
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
www.JacksonSoft.co.uk
|
|
|
|
|
Dear Coders,
I building a windows application using C#.I build a MDI.MDI has menu.i need when i press like F1(function keys) a specific form will open.This way i want to use F1...F12.Which event and how i do that.
Any suggestion is appreciated.Thanks in Advance.
Shafik
|
|
|
|
|
Clue: ShortcutKeys
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|