473,394 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

try-catches

Are try-catches mainly just used for code that could generate a
run-time error like division-by-zero, or the letter "A" for an
integer.

Someone said we could use try-catch in a situation, but since no
possible run-time error was involved, I used if-else statements
instead.

-
http://mysite.verizon.net/vze8adrh/news.html (profile) --Tim923 My email is valid.
Nov 16 '05 #1
9 1416
Hi Tim,

Yes Try Catch are mailly used for code that probably can generate some
exception and we can catch that exception and can take appropriate action, so
that application will not crash or behave unexpectedly.

Some times user throws his custom exceptions for some conditions in
application.

Manish
"Tim923" wrote:
Are try-catches mainly just used for code that could generate a
run-time error like division-by-zero, or the letter "A" for an
integer.

Someone said we could use try-catch in a situation, but since no
possible run-time error was involved, I used if-else statements
instead.

-
http://mysite.verizon.net/vze8adrh/news.html (profile) --Tim923 My email is valid.

Nov 16 '05 #2

"Tim923" <ju********@verizon.net> wrote in message
news:0u********************************@4ax.com...
Are try-catches mainly just used for code that could generate a
run-time error like division-by-zero, or the letter "A" for an
integer.

Someone said we could use try-catch in a situation, but since no
possible run-time error was involved, I used if-else statements
instead.


It's probably better to avoid using try catch if possible because an
exception is fairly resource intensive.

Michael Culley
Nov 16 '05 #3
Try/catches are used to handle situations that you expect could happen but
can't necessarily avoid. If you try to open a db connection, the server may
be down - so you may want to catch it and write the stuff to a queue so you
can get it later. However you shouldn't use try/catching in place of
checking for preconditions appropriately. Catching exceptions isn't cheap
so if you can use an IF statement to avoid them, by all means do it.

Jon Skeet has an excellent analysis of this on his site...let me see if I
can find the link.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Tim923" <ju********@verizon.net> wrote in message
news:0u********************************@4ax.com...
Are try-catches mainly just used for code that could generate a
run-time error like division-by-zero, or the letter "A" for an
integer.

Someone said we could use try-catch in a situation, but since no
possible run-time error was involved, I used if-else statements
instead.

-
http://mysite.verizon.net/vze8adrh/news.html (profile) --Tim923 My email

is valid.
Nov 16 '05 #4
Michael Culley <mc*****@NOSPAMoptushome.com.au> wrote:
Are try-catches mainly just used for code that could generate a
run-time error like division-by-zero, or the letter "A" for an
integer.

Someone said we could use try-catch in a situation, but since no
possible run-time error was involved, I used if-else statements
instead.


It's probably better to avoid using try catch if possible because an
exception is fairly resource intensive.


No it's not. Not if it's thrown reasonably infrequently. Exceptions
seem to be given an incredibly bad press for no particularly good
reason these days. While I certainly wouldn't recommend throwing them
within a tight loop, or indeed throwing them at all for a non-
exceptional condition, they're perfectly reasonable to throw for
genuine error situations where the method can't fulfil its contract.
They're a far safer way of indicating this than return codes.

In terms of performance, my laptop can throw 100,000 exceptions in a
second. I've seen people who haven't done such a test tell others that
throwing 2000 exceptions in an *hour* is probably harming their
performance significantly, which is rubbish. There are reasons for not
throwing 2000 exceptions in an hour, but performance isn't one of them.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
W.G. Ryan eMVP <Wi*********@NoSpam.gmail.com> wrote:
Try/catches are used to handle situations that you expect could happen but
can't necessarily avoid. If you try to open a db connection, the server may
be down - so you may want to catch it and write the stuff to a queue so you
can get it later. However you shouldn't use try/catching in place of
checking for preconditions appropriately. Catching exceptions isn't cheap
so if you can use an IF statement to avoid them, by all means do it.

Jon Skeet has an excellent analysis of this on his site...let me see if I
can find the link.


Do I? I'm not sure I have :)

I suspect you're referring to the test I did for finding a quick way of
parsing possibly-dodgy numbers.

Have a search for a thread entitled "Checking if a string can be
converted to Int32" on groups.google.com, in the
microsoft.public.dotnet.framework group.

Throwing an exception is certainly more expensive than not throwing an
exception - but if thrown at a reasonable frequency (which should be
low for reasons more important than performance) the performance
penalty should get lost in the noise.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
No it's not. Not if it's thrown reasonably infrequently.
So what you're really saying is that they are resourse intensive otherwise
you wouldn't have added the "reasonably infrequently". This function below
is around 3000 times slower without the first line. Of course you shouldn't
go out of your way to avoid exceptions but if it's reasonable to avoid them
like in the code below you may as well.

int ToInt32(object value)
{
if(value == DBNull.Value) return 0;
try
{
return Convert.ToInt32(value);
}
catch
{
return 0;
}
}
In terms of performance, my laptop can throw 100,000 exceptions in a
second. I've seen people who haven't done such a test tell others that
throwing 2000 exceptions in an *hour* is probably harming their
performance significantly, which is rubbish. There are reasons for not
throwing 2000 exceptions in an hour, but performance isn't one of them.


Damn you must have a *really* fast laptop. My p4 2.4 desktop couldn't get
anywhere near that. :-)

Michael
Nov 16 '05 #7
Michael Culley <mc*****@NOSPAMoptushome.com.au> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
No it's not. Not if it's thrown reasonably infrequently.
So what you're really saying is that they are resourse intensive
otherwise you wouldn't have added the "reasonably infrequently". This
function below is around 3000 times slower without the first line.


Only if you feed it DBNull all the time though, presumably... What
happens if you only feed it DBNull once in a million times, as might
reasonably the case in some applications? In that case, it could well
be *slower* to avoid the non-occurring exception in the common case
than the cost of the exception in the rare case.
Of course you shouldn't go out of your way to avoid exceptions
But there are times where it's possible to avoid it, but that *does*
involve going out of your way. On such occasions, I'd usually go with
the simpler code, unless I had any reason to believe it would be a
significant performance problem. For instance, I've shown elsewhere
that if you've got a lot of dodgy string data which is going to be
converted to ints, it's much faster to do a hard-coded check
beforehand. However, if I'm reasonably expecting the data to be
correct, I could avoid a *possible* exception by using that check, but
I wouldn't bother, because it would make the code more complicated for
very little (if any) performance gain.
but if it's reasonable to avoid them like in the code below
you may as well.

int ToInt32(object value)
{
if(value == DBNull.Value) return 0;
try
{
return Convert.ToInt32(value);
}
catch
{
return 0;
}
}


Yes, where possible it's worth avoiding them, but as much for stylistic
reasons as performance ones. If you end up getting to the situation
where you're throwing enough exceptions that it becomes a bottleneck in
your code, there's probably more wrong than just performance.
In terms of performance, my laptop can throw 100,000 exceptions in a
second. I've seen people who haven't done such a test tell others that
throwing 2000 exceptions in an *hour* is probably harming their
performance significantly, which is rubbish. There are reasons for not
throwing 2000 exceptions in an hour, but performance isn't one of them.


Damn you must have a *really* fast laptop. My p4 2.4 desktop couldn't get
anywhere near that. :-)


It's a P4 3.06GHz.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Only if you feed it DBNull all the time though, presumably... What
happens if you only feed it DBNull once in a million times, as might
reasonably the case in some applications? In that case, it could well
be *slower* to avoid the non-occurring exception in the common case
than the cost of the exception in the rare case.
It is just an example to show that in some cases exceptions are very
resource intensive. Compared to what you would expect an exception is very
slow. I agree this has probably been exaggerated a lot causing people to go
out of their way to avoid them.
But there are times where it's possible to avoid it, but that *does*
involve going out of your way. On such occasions, I'd usually go with
the simpler code, unless I had any reason to believe it would be a
significant performance problem. For instance, I've shown elsewhere
that if you've got a lot of dodgy string data which is going to be
converted to ints, it's much faster to do a hard-coded check
beforehand. However, if I'm reasonably expecting the data to be
correct, I could avoid a *possible* exception by using that check, but
I wouldn't bother, because it would make the code more complicated for
very little (if any) performance gain.


Quite possibly, that's something you have to consider for each situation.

Michael
Nov 16 '05 #9
Tim.... It is appropriate to throw an exception if the caller violates
the explicit
preconditions of the method, if the method is unable to maintain an
invariant,
or if the method cannot meet the stated postconditions.

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Regards,
Jeff
Someone said we could use try-catch in a situation, but since no

possible run-time error was involved, I used if-else statements
instead.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robert Brewer | last post by:
Alex Martelli wrote in another thread: > One sign that somebody has moved from "Python newbie" to "good Python > programmer" is exactly the moment they realize why it's wrong to code: > > ...
26
by: djw | last post by:
Hi, Folks- I have a question regarding the "proper" use of try: finally:... Consider some code like this: d = Device.open() try: d.someMethodThatCanRaiseError(...) if SomeCondition: raise...
19
by: White Wolf | last post by:
Hi, I cannot believe my eyes, but so far I could not find out what is the name of the construct, which is built from one try block and the catch clauses (handlers). I am not looking for a...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.