• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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

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
dgb
Asked:
dgb
3 Solutions
 
openshacCommented:
Is SaveChanges a public method, if so then throw the error, otherwise you probably want to handle it.
0
 
jpaulinoCommented:
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
 
dgbAuthor Commented:
openshac:
SaveChanges is a private function within a form
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TechTiger007Commented:


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
 
mkosbieCommented:
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
 
dgbAuthor Commented:
Thanks everybody
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now