Jump to content

OOP and errorhandling

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
Hello people.

I'm doing some OOP work now. finally. now I need to learn to think in this other way, and it isn't easy always.

Now I have come to an conclusion that I don't know what solution I should do, so I ask for help. I do it here in the General part, instead of PHP which I do the programming in, as it really is a design thing only.

I have a class, whatever it does, but in one om my methods, I try to perfom an complex action, and depending on the input data, it can have several different error types, how do I return the error typ easiest? I really want the method to return true on sucess and false on error, but how can I make it in a simple way, to avoid throwing exceptions to right or left, but still get the errors out of there? store the error in an member and create an method to read it out?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Depends on what you're trying to do. A common practice is to return an int, with zero for success and non-zero (an error code) for failure. However that's common for languages that don't support exception handling, if your chosen language does, I'd really suggest you use the provided exception handling mechanisms. That gives you a greater amount of control over the program and in a way it forces you to deal with the problem rather than silently ignoring it.
Wow I changed my sig!

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
Well, but if it's not that kinda errors, but to provide error messages sent to the user, depending on user input? I don't wanna use plain error message strings here either, as the application might need to support multi-language
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
any suggestions on that?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
relapse

relapse

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 476 posts
Return an array. The array can be associative and the first index be the error code (success or failure). You can store many things in the rest of the array.

if (array[0] == 0) // error
array[1] // error code
else 
// other code


#6
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
That might work, relapse, but who allocates the array? Who frees it? If it's on the stack, that means undefined behavior.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts

Aereshaa said:

That might work, relapse, but who allocates the array? Who frees it? If it's on the stack, that means undefined behavior.

It's php, you don't need to free memory there as in C/C++

but if it were in C++, your question is relevant...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
For C++, you can use a smart pointer to a vector.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog