Solved

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

Posted on 2008-10-23
6
193 Views
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")
else
   msgbox("Something went wrong")
endif

Method 2
try
   SaveChanges(param1,param2)
   msgbox("Saving succesfulll")
catch
   msgbox("Something went wrong")
end try

private sub SaveChanges()  (with try - catch and  throw error)    
or  
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.
0
Comment
Question by:dgb
[X]
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
6 Comments
 
LVL 6

Expert Comment

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

Accepted Solution

by:
jpaulino earned 170 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.
0
 

Author Comment

by:dgb
ID: 22784505
openshac:
SaveChanges is a private function within a form
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 165 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


0
 
LVL 8

Assisted Solution

by:mkosbie
mkosbie earned 165 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.
0
 

Author Closing Comment

by:dgb
ID: 31509131
Thanks everybody
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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