473,773 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception handling suggestions

Ok something simple like int.Parse(strin g) can throw these exceptions:
ArgumentNullExc eption, FormatException , OverflowExcepti on

I don't want my program to just crash on an exception, so I must
handle all of them. I don't care about which one happened, except to
write out exception.Messa ge to a log file. It seems verbose to write
out three handlers that all do the same thing. So, I could just catch
Exception. But, is that recommended? It'll catch everything, even
out of memory exceptions.

What do you suggest?

Zytan

Mar 10 '07
41 3077
Todos Menos [MSFT] wrote:
In my opinion; C# error handling sucks.

managed error handling sucks.

on error goto errhandler was and is, far superior-- in every fashion

I heard that we're removing TRY CATCH in the next version of Visual
Studio, it should be out next month
Then you are badly misinformed.
>
-Todos
On Mar 13, 3:06 pm, Göran Andersson <g...@guffa.com wrote:
>In my opinion, VB6 error handling was far inferior.

Shorter code is not always better code. What's difficult about
programming is not typing in the commands, so how much you have to type
has little impact on the efficiency of a programmer.

Besides, is it really more verbous?

This would roughly resemble the error handling in your VB6 example:

try {

} catch (IOException) {
} catch (FormatExceptio n) {
} catch (Exception ex) {

MessageBox.Show (ex.Message);

}

This is 108 characters compared to 158 (not counting tabs and line
breaks). Unless I have forgotten most of my math, that is much less, not
much more.

Handling different error types is built into the exception model, while
in VB6 you have to use a Select Case to determine the error type. Also
"IOException " says a lot more than "12345".

Exception handling fits well into the structure of the code, while in
VB6 error handling you have to jump back and forth in the code.
Spaghetti code is always harder to follow.

Todos Menos [MSFT] wrote:
>>in my opinion, vb6 error handler was much superior than this managed
crap.. i mean seriously-- it's a lot less verbose
Public Sub MySub
on error goto errhandler
CleanExit:
exit sub
ErrHandler:
Select Case Err.number
Case 12345, 1224334
resume cleanexit
case else
msgbox error$
resume cleanexit
end Select
End Sub
On Mar 10, 3:46 pm, "Zytan" <zytanlith...@y ahoo.comwrote:
Ok something simple like int.Parse(strin g) can throw these exceptions:
ArgumentNull Exception, FormatException , OverflowExcepti on
I don't want my program to just crash on an exception, so I must
handle all of them. I don't care about which one happened, except to
write out exception.Messa ge to a log file. It seems verbose to write
out three handlers that all do the same thing. So, I could just catch
Exception. But, is that recommended? It'll catch everything, even
out of memory exceptions.
What do you suggest?
Zytan
--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

--
Göran Andersson
_____
http://www.guffa.com
Mar 14 '07 #21
Todos Menos [MSFT] wrote:
then learn how to program bugfree?
If you truely believe that it is possible to learn how to write bugfree
code, then you have long way to go until you get to be a good programmer.
>
uh.. instead of writing pointers, write a friggin UI?

VB has had a superior interface for 15 years.. your C# crap was never
invented

-Todos


On Mar 13, 1:37 pm, Göran Andersson <g...@guffa.com wrote:
>Zytan wrote:
>>Note it uses
Application.T hreadException += new ...
instead of your
AppDomain.Cur rentDomain.Unha ndledException +=new ...
You mentioned ThreadException , but now I'm confused. Why do these
both do the same thing?
UnhandledExcep tion catches any unhandled exception in the application,
while ThreadException only catches unhandled exceptions that occur in
Windows Forms threads.

--
Göran Andersson
_____http://www.guffa.com


--
Göran Andersson
_____
http://www.guffa.com
Mar 14 '07 #22
On Mar 13, 11:24 pm, Göran Andersson <g...@guffa.com wrote:
Todos Menos [MSFT] wrote:
then learn how to program bugfree?

If you truely believe that it is possible to learn how to write bugfree
code, then you have long way to go until you get to be a good programmer.
He's not a programmer. He's an attention-starved teenager trying to
get a response--any response--to his trolling. Best to ignore him. Or
feel sorry for him. I think he needs a girlfriend.

Mar 14 '07 #23
bull mother fucking shit dude

it's possible to write bugfree code.
Microsoft has brainwashed you into accepting mediocrity
SERIOUSLY HERE KIDS

bugfree code is possible when you've got $60bn in cash

they just don't produce it

On Mar 13, 11:24 pm, Göran Andersson <g...@guffa.com wrote:
Todos Menos [MSFT] wrote:
then learn how to program bugfree?

If you truely believe that it is possible to learn how to write bugfree
code, then you have long way to go until you get to be a good programmer.


uh.. instead of writing pointers, write a friggin UI?
VB has had a superior interface for 15 years.. your C# crap was never
invented
-Todos
On Mar 13, 1:37 pm, Göran Andersson <g...@guffa.com wrote:
Zytan wrote:
Note it uses
Application.Th readException += new ...
instead of your
AppDomain.Curr entDomain.Unhan dledException +=new ...
You mentioned ThreadException , but now I'm confused. Why do these
both do the same thing?
UnhandledExcept ion catches any unhandled exception in the application,
while ThreadException only catches unhandled exceptions that occur in
Windows Forms threads.
--
Göran Andersson
_____http://www.guffa.com

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

Mar 14 '07 #24
I'm not misinformed.

I'm not the one that's using the trendy 'programming language of the
month'

just because C and Java had a MOTHER FUCKING _CRAP_ GUI for the past
10 years.. does that mean you should hop on the first language that's
competitive with VB?

VB won the war kid

Java -- I mean get real, Sun is worth what.. $3bn? PHP has 4 times the
following of Java..



On Mar 13, 11:09 pm, Göran Andersson <g...@guffa.com wrote:
Todos Menos [MSFT] wrote:
In my opinion; C# error handling sucks.
managed error handling sucks.
on error goto errhandler was and is, far superior-- in every fashion
I heard that we're removing TRY CATCH in the next version of Visual
Studio, it should be out next month

Then you are badly misinformed.


-Todos
On Mar 13, 3:06 pm, Göran Andersson <g...@guffa.com wrote:
In my opinion, VB6 error handling was far inferior.
Shorter code is not always better code. What's difficult about
programming is not typing in the commands, so how much you have to type
has little impact on the efficiency of a programmer.
Besides, is it really more verbous?
This would roughly resemble the error handling in your VB6 example:
try {
} catch (IOException) {
} catch (FormatExceptio n) {
} catch (Exception ex) {
MessageBox.Show (ex.Message);
}
This is 108 characters compared to 158 (not counting tabs and line
breaks). Unless I have forgotten most of my math, that is much less, not
much more.
Handling different error types is built into the exception model, while
in VB6 you have to use a Select Case to determine the error type. Also
"IOExceptio n" says a lot more than "12345".
Exception handling fits well into the structure of the code, while in
VB6 error handling you have to jump back and forth in the code.
Spaghetti code is always harder to follow.
Todos Menos [MSFT] wrote:
in my opinion, vb6 error handler was much superior than this managed
crap.. i mean seriously-- it's a lot less verbose
Public Sub MySub
on error goto errhandler
CleanExit:
exit sub
ErrHandler:
Select Case Err.number
Case 12345, 1224334
resume cleanexit
case else
msgbox error$
resume cleanexit
end Select
End Sub
On Mar 10, 3:46 pm, "Zytan" <zytanlith...@y ahoo.comwrote:
Ok something simple like int.Parse(strin g) can throw these exceptions:
ArgumentNullE xception, FormatException , OverflowExcepti on
I don't want my program to just crash on an exception, so I must
handle all of them. I don't care about which one happened, except to
write out exception.Messa ge to a log file. It seems verbose to write
out three handlers that all do the same thing. So, I could just catch
Exception. But, is that recommended? It'll catch everything, even
out of memory exceptions.
What do you suggest?
Zytan
--
Göran Andersson
_____http://www.guffa.com-Hide quoted text -
- Show quoted text -

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

Mar 14 '07 #25
I'm not mis-informed
i just don't accept VERBOSITY laying down

your try catch crap.. I mean seriously here.. what a waste of keyboard
effort

On Mar 13, 11:09 pm, Göran Andersson <g...@guffa.com wrote:
Todos Menos [MSFT] wrote:
In my opinion; C# error handling sucks.
managed error handling sucks.
on error goto errhandler was and is, far superior-- in every fashion
I heard that we're removing TRY CATCH in the next version of Visual
Studio, it should be out next month

Then you are badly misinformed.


-Todos
On Mar 13, 3:06 pm, Göran Andersson <g...@guffa.com wrote:
In my opinion, VB6 error handling was far inferior.
Shorter code is not always better code. What's difficult about
programming is not typing in the commands, so how much you have to type
has little impact on the efficiency of a programmer.
Besides, is it really more verbous?
This would roughly resemble the error handling in your VB6 example:
try {
} catch (IOException) {
} catch (FormatExceptio n) {
} catch (Exception ex) {
MessageBox.Show (ex.Message);
}
This is 108 characters compared to 158 (not counting tabs and line
breaks). Unless I have forgotten most of my math, that is much less, not
much more.
Handling different error types is built into the exception model, while
in VB6 you have to use a Select Case to determine the error type. Also
"IOExceptio n" says a lot more than "12345".
Exception handling fits well into the structure of the code, while in
VB6 error handling you have to jump back and forth in the code.
Spaghetti code is always harder to follow.
Todos Menos [MSFT] wrote:
in my opinion, vb6 error handler was much superior than this managed
crap.. i mean seriously-- it's a lot less verbose
Public Sub MySub
on error goto errhandler
CleanExit:
exit sub
ErrHandler:
Select Case Err.number
Case 12345, 1224334
resume cleanexit
case else
msgbox error$
resume cleanexit
end Select
End Sub
On Mar 10, 3:46 pm, "Zytan" <zytanlith...@y ahoo.comwrote:
Ok something simple like int.Parse(strin g) can throw these exceptions:
ArgumentNullE xception, FormatException , OverflowExcepti on
I don't want my program to just crash on an exception, so I must
handle all of them. I don't care about which one happened, except to
write out exception.Messa ge to a log file. It seems verbose to write
out three handlers that all do the same thing. So, I could just catch
Exception. But, is that recommended? It'll catch everything, even
out of memory exceptions.
What do you suggest?
Zytan
--
Göran Andersson
_____http://www.guffa.com-Hide quoted text -
- Show quoted text -

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

Mar 14 '07 #26
re: and a bitch to figure out
what on earth is going on when it doesn't.

oh does the widdle baby programmer have a hard time following a widdle
goto statement

oh how cute

IT IS A GOTO STATEMENT DUDE. HOW MUCH SIMPLER CAN YOU GET?
On Mar 13, 1:57 pm, "Bruce Wood" <brucew...@cana da.comwrote:
On Mar 13, 1:36 pm, "Todos Menos [MSFT]"

<todos_menos_m. ..@hotmail.comw rote:
in my opinion, vb6 error handler was much superior than this managed
crap.. i mean seriously-- it's a lot less verbose
Public Sub MySub
on error goto errhandler
CleanExit:
exit sub
ErrHandler:
Select Case Err.number
Case 12345, 1224334
resume cleanexit
case else
msgbox error$
resume cleanexit
end Select
End Sub

Well, yeah, sort of in the same way that GOTO is much less verbose
than all of this complicated for / foreach / while garbage. Don't even
get me started about all of those wordy function headers with
parameters and stuff... when you can just make everything global and
access it from anywhere. :-)

The problem with VB6 error handling is that at _any_ point in your
code, control can magically jump to your error handler subroutine
(hey, the syntax even _says_ goto), run some code, and then slip
quietly back into the control flow of your program as if nothing
happened.

Just like GOTO, it's great when it works, and a bitch to figure out
what on earth is going on when it doesn't. Like GOTO it can be used
responsibly to great effect, or horribly abused to create
incomprehensibl e code.

Been there, done that. I'm happier with the way things work in .NET,
thanks.- Hide quoted text -

- Show quoted text -

Mar 14 '07 #27
First off,

We should try and avoid exceptions where possible

And as far as int parsing try something like this

#region isInteger
/// <summary>
/// Determines if it is a valid integer.
/// </summary>
/// <param name="var"></param>
/// <returns></returns>
public static bool isInteger(strin g var)
{
double result;

return (Double.TryPars e(var,
System.Globaliz ation.NumberSty les.Integer,
System.Globaliz ation.NumberFor matInfo.Current Info,
out result));
}
#endregion

#region -- intFromString --
/// <summary>
/// Safely casts a string as integer. If the string is not a valid
int NOT_INITIALISED _INT is returned.
/// </summary>
/// <param name="var">stri ng to attempt parse on</param>
/// <param name="iDefault" >int - Default to use if the string is not
a valid int</param>
/// <returns>int</returns>
public static int intFromString( string var )
{
return intFromString( var, NOT_INITIALISED _INT );
}

/// <summary>
/// Safely casts a string as integer. If the string is not a valid
int the default value is used.
/// </summary>
/// <param name="var">stri ng to attempt parse on</param>
/// <param name="iDefault" >int - Default to use if the string is not
a valid int</param>
/// <returns>int</returns>
public static int intFromString( string var, int iDefault )
{
if( isInteger( var ) )
{
return int.Parse( var );
}

return iDefault;
}
#endregion

That should get you by safely.
And if you care to test that over 100k iterations against a try catch
you will see why it is worth the effort.

On the error handling front,
In a web application we use a HttpModule with a configurable level of
reporting

This allows us to essential switch it up to maximum reporting level
during the first phase of deployment.
This is obviously slower and puts a greater load on the server, but
allows us to track and remove bugs.

This is only my opinion
After 10 years of developing commercial systems that interact with 3rd
parties I have to say that exceptions
are unavoidable but predictable. You should not be looking to trap
developer errors, these should be caught and fixed during
UAT ( this also goes if you are writing libray code for other
people ).
Rather you should be looking to cater for errors in comms failure
between your external interfaces and 3rd party software
(this includes importing files )

Hope this helps

Mar 14 '07 #28
re:
Also as note... each try/catch block
is expensive so try to minimze them, and check your parameters before
you
execute.
yeah you're right

if Microsoft gave a damn about performance they never would have
invented .NET

On Mar 11, 7:23 am, "VJ" <nonewsaddr...@ yahoo.comwrote:
yes as Goran says its better to safe, than sorry.. but again what I gave is
unexpected situations at a global level.. That is provided by the .NET
environment, for windows applications. Also as note... each try/catch block
is expensive so try to minimze them, and check your parameters before you
execute.

For a console app.. not sure if there one such, my expertise is limited
there..sorry i cant help much.

VJ

"Zytan" <zytanlith...@y ahoo.comwrote in message

news:11******** **************@ j27g2000cwj.goo glegroups.com.. .
If the string really can be null, that is easy to check for. You should
check that before calling the method instead of waiting for the exception.

Actually, I just do:
int result = int.Parse("0" + s);
so, nulls are handled already.

I'm concerned with s == "ABC". The data that s can be may be warped
sometimes, and this is actually a decent spot to check to ensure it is
not warped.
If you don't really need to determine the exact reason why the string
can't be converted, you can use the int.TryParse method instead. Then
you will not need any exception handling at all.

Ah, ok, thanks, Göran!

Zytan

Mar 14 '07 #29
VJ <no***********@ yahoo.comwrote:
yes as Goran says its better to safe, than sorry.. but again what I gave is
unexpected situations at a global level.. That is provided by the .NET
environment, for windows applications. Also as note... each try/catch block
is expensive so try to minimze them, and check your parameters before you
execute.
try/catch blocks aren't expensive - it's only when exceptions are
*thrown* that there's any significant expense. Even that expense is
generally vastly overestimated by most people.

It's good practice to check parameters anyway, as then the *exact*
reason is apparent, and you can make sure that you don't "half"
complete an action. However, the correct response to incorrect
parameters is usually to throw an exception anyway...

See http://pobox.com/~skeet/csharp/exceptions.html and
http://pobox.com/~skeet/csharp/exceptions2.html
for more information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '07 #30

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

Similar topics

4
3137
by: zzfreddybb | last post by:
We are using HP aCC compiler on a HP Itanium box ( 11.23) We are having some severe performance hits using exception handling ( try/catch ) scenarios. The online aCC documentation says: HP aC++ exception handling has no significant performance impact at compile-time or run-time. We have not found this to be the case at all.
11
3275
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two "resources" I use are memory and file I/O and whenever there is a memory allocation failure or file I/O failure I just simply call a custom assert-type function to check, print a error message and abort. This seems to be OK for now (for the...
6
1905
by: Josh Mcfarlane | last post by:
I keep trying to get myself out of the return-code mindset, but it doesn't seem to work. They are suppose to get rid of if-then statements of return codes, but you still have to do an if statement once the code leaves your control (3rd party libraries, OS functions, etc) to throw an exception? Does anyone have any good suggestions on a book or website that deals with exception handling that could help turn this poor mind off of return...
7
1798
by: Dan Bass | last post by:
In a somewhat complex application, I've developed plug-in architecture and am having a problem as to when to catch general exceptions for logging purposes. In each plug-in class library, for example, I'm assuming it's throw on errors, and that the core then catches them. I've got proxy classes handling some fiddly details of the plug-ins. Should I allow the exception to bubble to the top most point it can in a call stack, then catch...
11
5600
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
2
2992
by: Petr Jakes | last post by:
I am a little bit confused by all possibilities for exceptions handling in Python (probably because I am not skilled enough??) I did try to search trough this list and reading Python tutorial about Errors and Exceptions but didn't find some "general" answer about exception handling policy (strategy). In the following example each row can IMHO raise an exception (if the Firebird service is not running for example, if the database is...
1
338
by: Yoav | last post by:
Hi, I have a web application in which I have implemented exception handling – my own custom exception etc. My question is - what do I do with exceptions in the Global.asax event? I don't want to redirect users to an error page - I want to keep unity in my exception handling (I don't do it for other exceptions). Any suggestions? Thanks
2
4047
by: jayapal | last post by:
Hi , I am using the NEW operator to allocate the memory in many places of my code.But I am not doing any error hadling or exception handling.Can any one suggests me how to do exception handling, which code part I have to add to do the exception handling Thanks in advance, ..
6
4398
by: Steve | last post by:
Hi All I have a windows forms Application (SAM) in vb.net 2008 using .net framework V2 One and only one customer out of 30 customers is getting errors daily where they have to close and restart my application, sometimes several times a day The customer has XP Home SP2
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.