logic_error not caught

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
//  SEE POSTS BELOW FOR UPDATE

#ifndef SRC_ERROR_HPP_
#define SRC_ERROR_HPP_

#include <stdexcept>
#include <string>

class Error : public std::logic_error
{
public:
	virtual const char* what() const noexcept
	{
		return "Well...";
	};

	virtual ~Error() noexcept {};

	double d_division(double x, double y) const
	{
		if(!y) throw logic_error("hi");
		else   return x / y;
	}

	int i_division(int x,int y) const
	{
		if(!y) throw Error{};
		else return x / y;
	}
};


#endif /* SRC_ERROR_HPP_ */ 


And the main :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Error.hpp"
#include <iostream>

int main()
{
	Error e;
	try
	{
		e.d_division(100.,0.);
		e.i_division(100 ,0);
	}
	catch(Error& e)
	{
		e.what();
	}
	catch(logic_error& l)
	{
		l.what();
	}
return 0;
}


Compiled with c++11 standart.

Errors received :
no matching function for call to ‘std::logic_error::logic_error()’
use of deleted function ‘Error::Error()’

Although explicitly calling std_logic_error("blabla") in the initializer list of Error() : logic_error("blabla")
works.

Why ?
Last edited on
As your class derives from logic_error, logic error would be created when your class is created. logic_error does not have a default constructor, so you cannot use it and need to explicitely call some other constructor. Coincidentally this is exactly what error message told you.
ok

I modified the source a little bit :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Error : public std::logic_error
{
public:
	explicit Error(const string& s = "Error") : logic_error(s) {}

	virtual const char* what() const noexcept
	{
		return "Well...";
	};
	virtual ~Error() noexcept {};

	double d_division(double x, double y) const
	{
		if(!y) throw logic_error("Double Division by 0");
		else   return x / y;
	}

	int i_division(int x,int y) const
	{
		if(!y) throw Error("Int Division by 0");
		else return x / y;
	}
};


Questions :
1. Still nothing gets caught, why ?
2. Now when the Error object is created, how can I use its logic_error subobject ?
Last edited on
1. Still nothing gets caught, why ?
It catches it. Why do you think it isn't?
2. Now when the Error object is created, how can I use its logic_error subobject ?
logic_error part. Yes, you can use it.
oh yes ... it catches it , it just doesn't display anything ... again why ?
Because... you are not telling it to disply anything.

Maybe this will show you:
1
2
3
4
void no_display()
{
    "You will not see me"; //For some reason this is not displayed
}
Here :

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	Error e;
	try
	{
		e.d_division(100.,0.);
	}
	catch(logic_error& e)
 	{
		e.what(); // shouldn't here output be "Double Division by 0" ?
	}
return 0;
}
shouldn't here output be "Double Division by 0" ?
No. Why would it. it is merely returning a pointer to c-string. Where would output it? To screen, file, send over network, to the memory mapeed device, to some periferial? You did not specified.

THis std::cout << e.what() would tell program to output it in standard output stream, but you do not have anything similar in your program.
Damn it ....
I totally forgot that what() doesn't call the cout or anything alike .
Making some tea breaks would be good...
sorry
Last edited on
Topic archived. No new replies allowed.