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?
OOP and errorhandling
Started by Orjan, Oct 12 2009 05:02 PM
7 replies to this topic
#1
Posted 12 October 2009 - 05:02 PM
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
|
|
|
#2
Posted 12 October 2009 - 05:25 PM
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
Posted 12 October 2009 - 05:31 PM
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
I study Information Systems at Karlstad University when I'm not on CodeCall
#4
Posted 15 October 2009 - 10:17 AM
any suggestions on that?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#5
Posted 17 October 2009 - 05:08 PM
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
Posted 20 October 2009 - 05:06 AM
That might work, relapse, but who allocates the array? Who frees it? If it's on the stack, that means undefined behavior.
#7
Posted 20 October 2009 - 05:25 AM
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
I study Information Systems at Karlstad University when I'm not on CodeCall
#8
Posted 20 October 2009 - 08:04 AM
For C++, you can use a smart pointer to a vector.


Sign In
Create Account

Back to top










