Error codes with bools!

Nov 30, 2009 at 11:59pm
Is it possible for me to create my own error codes? Such as if a function fails to accomplish it's task an error handler activates? I was thinking something like this, althought theoretically it sounds good for me, I'm not sure it would actually work;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {

bool success = myFunction();

if (success == true){
//code
//code
//more code...
}

else {
cout << "Error occured";
}
}
Last edited on Dec 1, 2009 at 12:00am
Dec 1, 2009 at 12:09am
That certainly does work. In that case, you'd just have your funciton return a bool.
Dec 1, 2009 at 12:34am
But in that case if my "myFunction()" had an error it would have to return a false bool. But the program would crash before it did that. How could I solve this?
Dec 1, 2009 at 12:58am
If a function crashes the program, there's nothing to do other than fix the function. The purpose of an error code is to give information about the result of a call under normal operation. For example, function that loads and displays a JPEG file on the screen could return NO_ERROR, FILE_NOT_FOUND, NOT_A_JPEG, BROKEN_FILE, etc. None of these are crashing conditions. The function is supposed to check the validity of the file.

Maybe what you're looking for are exceptions.
Dec 1, 2009 at 1:00am
What exactly do you mean by "had an error"? It wouldn't necessarily crash before it did that; that would be down to how well you wrote that fucntion.

This question is a little bit hazy I think without a situation, can you give us one to help explain more clearly?

EDIT: Too slow... bloody helios :(
Last edited on Dec 1, 2009 at 1:01am
Dec 1, 2009 at 1:11am
For example, I was trying to work on an ecryption program. When I compile it, no compiling errors occur. Despite the misfunctionality of the proper encryption (in other words, ignore it), when it comes to writing the "encrypted" string to a file it gives me a weird run-time error. Kind of the error that happens when you try to allocate to much memory. In this case you have the &bad_alloc but in my run-time errors I don't. So what did I try? To make an error handler based on boolean values. I already deleted the source code of the program since it simply didn't work (damn, I screwed on that one). But in any general case, when a runtime error occures, can't I fix it with boolean values?
Dec 1, 2009 at 1:19am
I don't get it. "Fix" it? With bools? I'm just not following.

In any case, it sounds like you had some serious bugs, there. A buffer overflow has nothing to do with error or exception handling. That's something you have to fix yourself.
Dec 1, 2009 at 2:55am
Ok, then let's take your example. What if "FILE_NOT_FOUND" happened, how could I fix that?
Dec 1, 2009 at 3:19am
well your function wouldn't fix it, it would just report the error. And the part of the code that called the function would check for that error.

1
2
3
4
if( myFunction() == FILE_NOT_FOUND )
{
  cout << "Error, the file wasn't found";
}
Dec 1, 2009 at 3:28am
Ok, I think I exaplained myself badly in my last post. I don't want to fix the FILE_NOT_FOUND, but how could I define it? I mean, the compiler won't assume itself that FILE_NOT_FOUND refers to that the file the person asked for doesn't exist.
Dec 1, 2009 at 3:37am
You put it in your comments and/or make a constant/enum for it.
Dec 1, 2009 at 4:00am
Can you please explain your post better firedraco?
Dec 1, 2009 at 4:05am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum ERROR
{
  OK,
  FILE_NOT_FOUND,
  SOME_OTHER_ERROR
};


ERROR MyFunction()
{
 // blah blah
  if( ... )
    return FILE_NOT_FOUND;

  else if (...)
    return SOME_OTHER_ERROR;

  return OK;
}
Last edited on Dec 1, 2009 at 4:05am
Dec 1, 2009 at 6:28am
Yes, I understand that, but what I mean is what I should put inside that if statement to return "FILE_NOT_FOUND"?
Dec 1, 2009 at 4:07pm
Try to open the file. Then, if (!file), assuming you are using the standard fstreams.
Dec 1, 2009 at 6:35pm
@Zhuge: We are talking about his custom functions.

You can't, you have to compare it directly.

You could however, create a function inside called "encryptSucceeded()" or something similar that does all the checking of the special things for you.

In the end however, you have to require the user to do something special.
Topic archived. No new replies allowed.