473,951 Members | 31,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning an error message when something goes wrong

Hi everyone,

I'm wondering what you're supposed to do when you call a method that is
required to perform some action but that action subsequently fails. More
specifically, how do you get a custom message back so that you can provide
some useful information to the application user?

It seems we dont have much room for manouever.

Example:
I might have a method called load file which is supposed to return a file
object. Inside this method a problem is detected and the file object can't
be loaded correctly.

What do I do?

Do I return an empty file object and just accept the fact that I cant return
helpful info to the user?
Do I throw an exception even though the file not being loaded isnt really an
exceptional occurence?
Or is there some other approach i can use.

The only way i can see to return context based error messages is to throw an
exception. However, I don't see how you can reconcile the use of exceptions
in non-exceptional circumstances given the general wisdom that says you
should only throw exceptions when something truly unexpected has happened.

Thanks in advance for any advice you can offer.

Kindest Regards

Simon


Nov 16 '05 #1
7 1417
Simon,

You could always return an error code, an integer (or value from an
enumeration) indicating what the status is. This would allow you to
indicate a good number of situations, which should suit your needs.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Simon" <sh856531@micro softs_free_emai l_service.com> wrote in message
news:eg******** ******@TK2MSFTN GP14.phx.gbl...
Hi everyone,

I'm wondering what you're supposed to do when you call a method that is
required to perform some action but that action subsequently fails. More
specifically, how do you get a custom message back so that you can provide
some useful information to the application user?

It seems we dont have much room for manouever.

Example:
I might have a method called load file which is supposed to return a file
object. Inside this method a problem is detected and the file object can't
be loaded correctly.

What do I do?

Do I return an empty file object and just accept the fact that I cant
return
helpful info to the user?
Do I throw an exception even though the file not being loaded isnt really
an
exceptional occurence?
Or is there some other approach i can use.

The only way i can see to return context based error messages is to throw
an
exception. However, I don't see how you can reconcile the use of
exceptions
in non-exceptional circumstances given the general wisdom that says you
should only throw exceptions when something truly unexpected has happened.

Thanks in advance for any advice you can offer.

Kindest Regards

Simon

Nov 16 '05 #2
Hi Simon

Another perspective - you can consider that an Exception should be raised
when the calling code has violated the implicit assumptions of your class.

Imagine that you have an argument on a method which allows you to set the
age of a person in years. For efficiency, you choose to use an int to
represent that value. An implicit assumption of your class which isn't
covered by the datatype int, is that the age of a person in years should have
a maximum value of say 130.

If a client of your class calls the method passing through the value 250,
then you should raise an Exception - the implicit assumption of your class
has been violated...the exception should be raised with an appropriate
message so that the developer who is using your class can be told why the
method call wasn't appropriate.

Note that in this case, it's not a circumstance that may happen infrequently
(which is how many people think of exceptions), but it's when the data
supplied does not conform to the internal assumptions that your class makes.

I think this is Richter's view of exceptions. Other views??

Nigel Armstrong

"Simon" wrote:
Hi everyone,

I'm wondering what you're supposed to do when you call a method that is
required to perform some action but that action subsequently fails. More
specifically, how do you get a custom message back so that you can provide
some useful information to the application user?

It seems we dont have much room for manouever.

Example:
I might have a method called load file which is supposed to return a file
object. Inside this method a problem is detected and the file object can't
be loaded correctly.

What do I do?

Do I return an empty file object and just accept the fact that I cant return
helpful info to the user?
Do I throw an exception even though the file not being loaded isnt really an
exceptional occurence?
Or is there some other approach i can use.

The only way i can see to return context based error messages is to throw an
exception. However, I don't see how you can reconcile the use of exceptions
in non-exceptional circumstances given the general wisdom that says you
should only throw exceptions when something truly unexpected has happened.

Thanks in advance for any advice you can offer.

Kindest Regards

Simon


Nov 16 '05 #3
Hello!

Good explanation!
I think this is Richter's view of exceptions. Other views??


Any links to these resources?!

Thanks in advance.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #4
Thanks guys. Thats all good stuff.

Additional question:

How do you manage the proliferation of exception throwing classes if you use
this approach.

Seems to me that all classes have internal assumptions that might result in
an exception being thrown. Surrounding everything in a tcf block isn't
really a sensible option so can anyone suggest a decent approach to making
sure that you catch exceptions without them bringing the application down?

Thanks again all

Simon
Nov 16 '05 #5
Simon...

I argue that the decision to return an error code or throw an exception
in a method that opens a file is dependent on the explicitly stated
pre-conditions of the method. If a pre-condition of a method that opens
a file is that the file must exist, then the method should throw an
exception if the file does not exist. Alternatively, if there is no
stated pre-condition that the file must exist then the method should
return an "error code" on failure to find a file. For instance, the
method might return false on failure to find a file.

Regards,
Jeff
Seems to me that all classes have internal assumptions that might

result in an exception being thrown.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
Hi Simon

This is a problem that thankfully we don't have to deal with in the same way
that our J*** friends do. In Java, the majority of exceptions are checked
exceptions. So when you try to open a file, you have to write a try catch
block to handle the IO Exception that might occur, or modify the signature of
the method to state that it may throw an IO Exception. In .NET, if you choose
not to write a try catch, then the Exception propagates up the call stack
until the app exits, which means that you have the chance to write a central
Exception handler if you so choose...it's a situation that Java has only just
addressed, and for those of us who have / are using both environments, it's
clear that the .NET way of doing things is better!!

It also depends on what sort of app you are writing - Windows Forms and
ASP.NET applications have different ways of writing the central handler.

HTH

Nigel Armstrong
"Simon" wrote:
Thanks guys. Thats all good stuff.

Additional question:

How do you manage the proliferation of exception throwing classes if you use
this approach.

Seems to me that all classes have internal assumptions that might result in
an exception being thrown. Surrounding everything in a tcf block isn't
really a sensible option so can anyone suggest a decent approach to making
sure that you catch exceptions without them bringing the application down?

Thanks again all

Simon

Nov 16 '05 #7
"Simon" <sh856531@micro softs_free_emai l_service.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Seems to me that all classes have internal assumptions that might result in an exception being thrown. Surrounding everything in a tcf block isn't
really a sensible option so can anyone suggest a decent approach to making
sure that you catch exceptions without them bringing the application down?


Simple.... Catch the exceptions at the level where you can deal with
them. For the most part, conditions that throw an exception would prevent
the application from continuing anyway. So, most of the time, you just want
to catch all exceptions at the highest level, and output a pretty messages
before shutting down. (The exception itself should contain sufficient
information on what failed & why)

If a exception is a condition that you can recover from (and if so,
reconsider using an exception), catch it at the level you can deal with it &
retry.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com


Nov 16 '05 #8

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

Similar topics

8
426
by: matt breedlove | last post by:
(x-posted to microsoft.public.dotnet.general and microsoft.public.dotnet.languages.vb) Hello all. I am currently creating a Windows Service using VB.NET and would like to return an error if something goes wrong during startup, IE: parsing a configuration file. The only way I know of is to throw an exception, and this will prevent the
9
1173
by: Simon | last post by:
Hi everyone, I'm wondering what you're supposed to do when you call a method that is required to perform some action but that action subsequently fails. More specifically, how do you get a custom message back so that you can provide some useful information to the application user? It seems we dont have much room for manouever. Example:
4
3111
by: Robert Schuldenfrei | last post by:
Dear NG, I was about to "improve" concurrency checking with a Timestamp when I discovered that my current code is not working. After about a day of beating my head against the wall, I am turning to the NG in hopes that someone can spot what I am doing wrong. Key to this technique working is the SQL UPDATE statement. It is designed to fail
3
4257
by: Moshe Kravchik | last post by:
Hi! We have a Web Service written in ATL Server and a client written in Java using Axis. When something goes wrong on the server side, it returns an HRESULT of the error which is translated into the SoapFault. The problem is that the SoapFault is interpreted by Axis not as a SoapFaultException, but as a more generic AxisFault exception with all the info we put in the soap fault lost. Has anyone manage to get this to work? Please...
23
5994
by: Peter | last post by:
I have a problem with a page show_image.asp that returns a jpg image under Windows XP Pro SP2. The page sets content type as: Response.ContentType = "image/jpg" While this works perfectly fine on most machines, on some machines I experience this problem: When loading the page a window pops up that asks if I want to open the document show_page.asp. When I click "Open" Interdev pops up and opens up a
0
10172
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
11602
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
11195
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...
1
11371
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,...
0
9904
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
7444
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();...
0
6353
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4967
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
4554
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.