Discussion: what is the proper way to handle error's

Posted on 2008-10-23
Medium Priority
Last Modified: 2013-11-05
Hi I am having a discussion what the best way is to handle error's,

Method 1
if SaveChanges(param1,param2) then
   msgbox("Saving succesfulll")
   msgbox("Something went wrong")

Method 2
   msgbox("Saving succesfulll")
   msgbox("Something went wrong")
end try

private sub SaveChanges()  (with try - catch and  throw error)    
private function SaveChanges as boolean   (with try - catch and return value)

It is also possible that one method isn't better than the other, just different way of handeling things.
Question by:dgb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 22784348
Is SaveChanges a public method, if so then throw the error, otherwise you probably want to handle it.
LVL 48

Accepted Solution

jpaulino earned 510 total points
ID: 22784493
It's the same, but the Try ... Catch ... End Try method can handle some unexpected errors that you may have.
I use bought methods and only depends of the place. For example in your second example if you want to write code after the SaveChanges() function and just display a waning message, it would be prefer to use the first one.
Bought are valid methods.

Author Comment

ID: 22784505
SaveChanges is a private function within a form

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 13

Assisted Solution

TechTiger007 earned 495 total points
ID: 22784664

You can catch the exception and first log the exception message and stack trace to a log file. Then return false or throw a new exception with user readable message . In the calling function you can display this message to the user or do anything else. It is always better to handle exceptions in same function where ever possible, one method should not expect its calling method to handle the exception, that will improve re usability. So I would suggest you to return boolean


Assisted Solution

mkosbie earned 495 total points
ID: 22784947
It depends on what you need the error checking for.  Throwing/handling an exception is FAR more versatile:
  - You can handle multiple error types (eg out of memory, invalid argument)
  - You can attach a message to the error
  - You have access to a stack trace
  - You can catch/handle errors you didn't anticipate (eg null pointer)
  - You can normally interpret and attempt to correct the error in code (rather than relying on the user)

A boolean return provides none of those.  The added functionality doesn't always mean you should jump to throwing/handling errors.  If in your method you only care about whether or not the changes worked, and don't care why, a boolean is a better option.  The boolean is generally faster and easier to code with.

Author Closing Comment

ID: 31509131
Thanks everybody

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question