473,396 Members | 1,990 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,396 software developers and data experts.

When should I throw an Exception

When you write code, you can generally structure it so that you handle all
the errors, so when is it most appropriate to Throw an exception rather than
coding for error handling in a more granular sense.
Nov 27 '05 #1
3 1183
Ive just read a lot on this and there is guidence, principly if something
does not do what it is intended to do then this should be regarded as a
system failure and an exception should be thrown.

--
Best Regards

The Inimitable Mr Newbie º¿º
"Mr Newbie" <he**@now.com> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
When you write code, you can generally structure it so that you handle all
the errors, so when is it most appropriate to Throw an exception rather
than coding for error handling in a more granular sense.

Nov 27 '05 #2
But more granularly, you should only throw an exception when something
exceptional happens :)

As a general rule, in each routine I write I try to catch and handle only
the exceptions that I can actually do something about in that routine. If it
is an exception that is thrown because of something that is clearly outside
the scope of that exact routine then I either just let the exception bubble
upwards, or sometimes I catch it, log it, and then rethorw it (using only
the Throw function) being careful to preserve the stack.

Try

DoSomehtingHere()

Catch ex As SpecificException
HandleTheSpecificExpetion()

Catch ex As Exception
LogTheException()
Throw 'rethrow the exception upwards and preserve the call stack (DO NOT
USE 'Throw ex' here)

Finally
DoMyCleanUpHere()

End Try

"Mr Newbie" <he**@now.com> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
Ive just read a lot on this and there is guidence, principly if something
does not do what it is intended to do then this should be regarded as a
system failure and an exception should be thrown.

--
Best Regards

The Inimitable Mr Newbie º¿º
"Mr Newbie" <he**@now.com> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
When you write code, you can generally structure it so that you handle
all the errors, so when is it most appropriate to Throw an exception
rather than coding for error handling in a more granular sense.


Nov 27 '05 #3
Thanks for your advice.

--
Best Regards

The Inimitable Mr Newbie º¿º
"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:ur**************@TK2MSFTNGP12.phx.gbl...
But more granularly, you should only throw an exception when something
exceptional happens :)

As a general rule, in each routine I write I try to catch and handle only
the exceptions that I can actually do something about in that routine. If
it is an exception that is thrown because of something that is clearly
outside the scope of that exact routine then I either just let the
exception bubble upwards, or sometimes I catch it, log it, and then
rethorw it (using only the Throw function) being careful to preserve the
stack.

Try

DoSomehtingHere()

Catch ex As SpecificException
HandleTheSpecificExpetion()

Catch ex As Exception
LogTheException()
Throw 'rethrow the exception upwards and preserve the call stack (DO
NOT USE 'Throw ex' here)

Finally
DoMyCleanUpHere()

End Try

"Mr Newbie" <he**@now.com> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
Ive just read a lot on this and there is guidence, principly if something
does not do what it is intended to do then this should be regarded as a
system failure and an exception should be thrown.

--
Best Regards

The Inimitable Mr Newbie º¿º
"Mr Newbie" <he**@now.com> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
When you write code, you can generally structure it so that you handle
all the errors, so when is it most appropriate to Throw an exception
rather than coding for error handling in a more granular sense.



Nov 27 '05 #4

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

Similar topics

0
by: Mike Schilling | last post by:
I have some code that calls methods reflectively (the method called and its parameters are determined by text received in a SOAP message, and I construct a map from strings to MethodInfos). The...
6
by: Arjen | last post by:
Hi, I'm reading the enterprise library documentation and there I see the throw statement. try { // run code } catch(Exception ex) {
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
2
by: Gummy | last post by:
Hello All, I have a webpage that has two dropdown listboxes. Based on what is selected in these dropdown listboxes, it filters a DataGrid . That works fine. In the DataGrid , when I go to edit...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
1
by: Elliot | last post by:
When decrypt the xml, output "Unable to retrieve the decryption key." Can anyone help me solve the problem? I got the code from http://msdn.microsoft.com/en-us/library/ms229746.aspx using...
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
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: 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
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,...
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
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,...
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.