exceptions instead of return parameters | Bytes (2024)

Home Posts Topics Members FAQ

Dan Holmes

In one of the other threads it was mentioned that using return values
from methods to indicated success/failure of the method should be
replaced with throwing exceptions. Which would mean code like:

List l = object.Find(... );
if (l == null) ... //nothing returned

would be replaced with:

try{
List l = object.Find(... );
....
}
catch (NotFoundExcept ion ex)
{
}
catch (Exception ex)
{
}

Is this the preferred method? And what is the background on this?
dan

Apr 19 '06

Subscribe Reply

15 exceptions instead of return parameters | Bytes (1) 1757 exceptions instead of return parameters | Bytes (2)

  • <
  • 1
  • 2

Jon Skeet [C# MVP]

bombdrop <sr********@yah oo.co.uk> wrote:

It is in my opion better to have a function return a bool for success
or faliure than throwing an exception. Exceptions are resource intesive
so where possible. try something like this

Just out of interest (and before following the link below) - just how
resource intensive do you think exceptions are? How many exceptions do
you think you could throw per second? How often would you have to throw
them in order to actually see significant performance loss?

A ValidateLogin method probably *should* return a boolean, because its
whole purpose is to verify data. The username and password being
incorrect isn't a failure situation - it's perfectly normal. Any other
error, however - whether database-related or not - should result in
exceptions, IMO.

See http://www.pobox.com/~skeet/csharp/exceptions.html for more on
this.

--
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

Apr 19 '06 #11

Jon Skeet [C# MVP]

Bruce Wood <br*******@cana da.com> wrote:

<snip>

The only general rule I would put forth would be that if you expect
your callers to be testing for something a lot, you should offer them a
return value (or another method like TryParse) rather than forcing them
to put try...catch everywhere. try...catch should be an uncommon
construct in client programs, not because "it's slow" (it's not,
terribly), but because it interrupts normal control flow and makes the
code difficult to follow.

Spot on. I couldn't have (and plainly haven't!) said it better myself.

--
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

Apr 19 '06 #12

Josh Twist

Great post Dan, I've been meaning to blog on this for a while, so
here's my tuppence.

http://www.thejoyofcode.com/Exceptions.aspx

Apr 19 '06 #13

wfairl

He's trying to return an object from his find method, not a bool on
whether it's found or not. He needs an "exists" type method on this
class but I definitely think the find method needs to throw an
exception similar to an IndexOutOfRange exception with arrays.
Otherwise there's no way to differientiate between an existing null
item and a non-existent item.

Apr 19 '06 #14

Josh Twist

Hey wfairl,

I see your point - but my blog entry isn't a direct answer to this post
but a general steam off on the subject.

IMO it all comes back to that implicit assumptions definition I quoted
from Richter.

Your proposition is only valid if there is someway that the things you
are looking for with the Find thing could actually be null, which would
be unusual. The nearest example I can think of is ASP.NET's FindControl
method, this returns null if nothing is found, but then you can't add
Null to the controls collection so that makes sense.

Hopefully Dan's got enough AMMO now to go on and make an informed
decision using his greater knowledge of the requirement and audience.

I beginning to think a *consistent* approach to this particular issue
might be best i.e. always throwing exceptions, but also always provide
a TryGetValue for developers ready to deal with the possibility of a
null return... food for thought anyway. T

Apr 19 '06 #15

Nick Hounsome

<wf****@gmail.c om> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .

He's trying to return an object from his find method, not a bool on
whether it's found or not. He needs an "exists" type method on this
class but I definitely think the find method needs to throw an
exception similar to an IndexOutOfRange exception with arrays.
Otherwise there's no way to differientiate between an existing null
item and a non-existent item.

There is always "bool Find(object key,out object value)" .
This is probably the most logically correct but unappealing approach.

Apr 20 '06 #16

  • <
  • 1
  • 2

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

Similar topics

33 3031

spliting a list by nth items

by: Steven Bethard |last post by:

I feel like this has probably been answered before, but I couldn't find something quite like it in the archives. Feel free to point me somewhere if you know where this has already been answered. I have a list in a particular order that I want to split into two lists: the list of every nth item, and the list of remaining items. It's important to maintain the original order in both lists. So the first list is simple:

Python

5 1397

by: John |last post by:

Hi, In my years as a VB programmer, I have settled into this pattern of creating collections classes, with an AddNew() method. AddNew() validates the parameters, instantiates the object, adds it to the collection, and returns it. The AddNew() method was used to get around the lack of a constructor in VB classes. Now I was just about to rewrite this same pattern in C#, when I realized hey ... I've got constructors, and instead of...

.NET Framework

59 4453

C++ Exceptions Cause Performance Hit?

by: kk_oop |last post by:

Hi. I wanted to use exceptions to handle error conditions in my code. I think doing that is useful, as it helps to separate "go" paths from error paths. However, a coding guideline has been presented that says "Use conventional error-handling techniques rather than exception handling for straightforward local error processing in which a program is easily able to deal with its own errors." By "conventional error-handling," I believe...

C / C++

1 2918

Exceptions from ESQL/C to DB2

by: N.V.Dev |last post by:

Hi, I have written an ESQL/C program and should any error occur; the same should be displayed as error message. Wrote a test script and expected an exception but DB2 did not throw any such exception rather displayed return = 0 Below is the snippet for ESQL/C #include <stdio.h>

DB2 Database

15 426

Implementing exceptions of C++ in C

by: Bernard |last post by:

Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions have been asked on clc before so it is probably OK. I am a newbie to C++. BS 3rd edition states: % The throw transfers control to a handler for exceptions .... %

C / C++

1 2390

Understanding and Using Exceptions

by: Anonieko |last post by:

Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I look for when evaluating someone's code is a try/catch block. While it isn't a perfect indicator, exception handling is one of the few things that quickly speak about the quality of code. Within seconds you might discover that the code author...

ASP.NET

11 1740

Avoiding Exceptions

by: Jonathan Wood |last post by:

I must be the only one who doesn't think exceptions are the greatest thing since bread. Consider the following code: id = Convert.ToInt32(Request.QueryString); First, is there an easy way to tell which methods or properties could potentially throw an exception? I've scanned the docs and it doesn't seem to say. Without that knowledge, it's a bit of hit and miss.

C# / C Sharp

13 2569

Should I use exceptions instead of error codes?

by: mike3 |last post by:

Hi. (crossposted because the program is in C++ and some C++-related elements are discussed, hence comp.lang.c++, plus general program design questions are asked, hence comp.programming.) I'm making this bignum package in C++. I'm wondering though on how to handle the errors. Right now most operations routines return error codes if they fail -- but some routines they use inside them, or overloaded operators, will throw exceptions on...

C / C++

17 1745

Missing exceptions in PEP 3107

by: Christoph Zwerschke |last post by:

I'm just reading PEP 3107 (function annotations) and wonder why exceptions are not mentioned there. I think it would be helpful if one could specify which exceptions can be raised by a function, similarly to how it is possible in C++ using the "throw" clause. The syntax would be something like this: def foo(a: expr, b: expr = 5) raises expr -expr: The expr in that "raises" clause should be a list of Exceptions.

Python

9663

What is ONU?

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...

General

9511

Changing the language in Windows 10

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,...

Windows Server

1 10136

The easy way to turn off automatic updates for Windows 10/11

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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...

Windows Server

9016

AI Job Threat for Devs

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...

Career Advice

1 7525

Access Europe - Using VBA to create a class based on a table - Wed 1 May

by: isladogs |last post by:

The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...

Microsoft Access / VBA

6765

Couldn’t get equations in html when convert word .docx file to html file in C#.

by: conductexam |last post by:

I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

C# / C Sharp

5548

Windows Forms - .Net 8.0

by: adsilva |last post by:

A Windows Forms form does not have the event Unload, like VB6. What one acts like?

Visual Basic .NET

1 4090

transfer the data from one system to another through ip address

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

C# / C Sharp

2 3695

How to add payments to a PHP MySQL app.

by: muto222 |last post by:

How can i add a mobile payment intergratation into php mysql website.

PHP

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

To disable or enable advertisem*nts and analytics tracking please visit the page.

exceptions instead of return parameters | Bytes (2024)

References

Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 5888

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.