Runtime_error?

I'm trying to catch a runtime error. When I run my program under certain circumstances I get a box which tells me that there has been a 'Runtime error' though when I add the below to my error handler, it doesn't seem to catch the error.

If I use the catch(...), this works. It catches an error though I can't see what it is. If my system is telling me that I have a runtime error then why can't I catch it? Is there a way to resolve this? Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
try {
//My code
}

catch(std::runtime_error &ers)
    {
         ofstream mf;
         mf.open("Error.txt", ios::out|ios::binary);
         string serror = ers.what();
         mf << serror;
         mf.close();

    }
What code are you running?
It looks like you error isn't a std::runtime_error.

If you catch all, and run it in a debugger, you'll be able see the error.

BTW, that code is excessively verbose and ignores a simple truth; that an object is initialised by its constructor and cleaned up by its destructor.
1
2
3
4
5
    catch (const std::exception& e)
    {
         std::ofstream mf("Error.txt", ios::out|ios::binary); // do you really mean binary?
         mf << e.what() << std::endl;
    }
Last edited on
Hi
I can't actually put the code up. I'm not really interested in fixing that, what I'm trying to do is catch all kinds of errors which could appear. So I am using this error to determine how to resolve something like this.

I've tried the below already. Maybe I'm missing another type of error? I'm not sure.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 catch(std::runtime_error &ers)
    {
         ofstream mf;
         mf.open("Error.txt", ios::out|ios::binary);
         string serror = ers.what();
         mf << serror;
         mf.close();

    }

catch(std::exception &ers)
    {
         ofstream mf;
         mf.open("Error.txt", ios::out|ios::binary);
         string serror = ers.what();
         mf << serror;
         mf.close();

    }


 catch(std::out_of_range &ers)
    {
         ofstream mf;
         mf.open("Error.txt", ios::out|ios::binary);
         string serror = ers.what();
         mf << serror;
         mf.close();

    }

 catch(int &ers)
    {
         ofstream mf;
         mf.open("Error.txt", ios::out|ios::binary);
         mf << ers;
         mf.close();

    }

catch(std::string &ers)
    {
         ofstream mf;
         mf.open("Error.txt", ios::out|ios::binary)
         mf << ers;
         mf.close();

    }

 catch(...)
    {
         ofstream mf;
         mf.open("Error.txt", ios::out|ios::binary);
         string serror = "unknown error";
         mf << serror;
         mf.close();

    }

True, I didn't need to open in binary form.
But if the error was caught, a file named Error.txt would be created and this is not happening.

This happens during runtime.
You should just use the
const std::exception& e
as kbw suggested.

If you take a look at the std::runtime_error class you will see that it only has 4 direct sub classes and non of those are sub classed. So unless your exception is a runtime exception (i.e. of the type specified by the 4 classes or runtime_error), then runtime_error is not able to catch that exception.
http://www.cplusplus.com/reference/stdexcept/runtime_error/

You are most likely coming from a java background where java.lang.RuntimeException can be used to catch most runtime exceptions, but in C++, it is a bit different

Last edited on
Thanks, I'm actually not good at error trapping at all. No Java background either.

I'll just use std::exception and see how it goes. Here's the error in the debugger. The relevant error part anyway. Error 03 ?



1
2
3
4
5
6
7
8
9
10
Continuing...

[debug]> cont
[debug]Continuing.
[debug][Inferior 1 (process 2524) exited with code 03]
[debug]>>>>>>cb_gdb:

[Inferior 1 (process 2524) exited with code 03]

[debug]> quit
Topic archived. No new replies allowed.