Making own exception out of std::logic_error

Hi,

I am trying to define my own exceptions derived from logic_error.


I am trying to make two exceptions underflow and overflow.

Underflow will be thrown if class.a<0,
for class.a = class.b-class.c

Overflow will be thrown if class.a>MAX,
class.a= class.b+class.c
(MAX is the largest "class.a" can be)

(They might already exist but assignment is to make your own exceptions)


So far this is what I come up with,
Am I too far away or close?
I could not find enough info about logic_error, and the ones I found
do not talk about deriving your own exception.

This is what I am trying to do
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
#include <iostream>
#include "Class.h"// header file for the class
#include <stdexcept>
#include "Overflow.h"
using namespace std;
using namespace cs52;


void main(){


try {
 
  Class a = b - c;
  cout << "Try catch not working" << endl;

} catch( UnderFlow ) {

cout<<"UnderFlow working"; 

} catch( std::logic_error ) {

// the catch above should have caught this error, not this one!
  cout << "UnderFlow not working" << endl;

}
}



Header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef UNDERFLOW_H
#define UNDERFLOW_H
#include <string>
#include <iostream>
#include <stdexcept>

using namespace std;

namespace cs52{

class UnderFlow:public logic_error {

public:
	UnderFlow();
	virtual void string what();// or something else here?


protected:
//Do I need something protected here?	

};

}
#endif 


Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>
#include <stdexcept>
#include "Underflow.h"

using namespace std;

namespace cs52{

UnderFlow::UnderFlow(): logic_error()//What should I write as argument for logic_error
{}

string UnderFlow::what(){}


}


Another header try,
this one is for OverFlow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef OVERFLOW_H
#define OVERFLOW_H
#include <string>
#include <iostream>
#include <stdexcept>

using namespace std;

namespace cs52{

class OverFlow:public logic_error {

public:
	OverFlow(string what_arg);
	const char* what(what_arg) 
	{return "OverflowingFlashDriveException\n";}
	//virtual void string what();

};


}
#endif 



Thank you in advance.
The exceptions classes don't need to be elaborate. You can put what you like in there. It all depends on what information you want to get out of them.

If all you want to know is that you had an overflow or underfow, they don't need any state of their own. If fact, there is already a std::overflow_error and std::underflow_error that you can use.

If you need state, for example the parameters that were used when the overflow,underflow occured, then you need to write your own, but make them derive from std::overflow_error and std::underflow_error respectively instead of logic_error (as they are in fact, runtime_errors not logic_errors).
Last edited on
Our professor for some reason wants us to use logic_error.
I figured out overflow and underflow already exist but, assignment is to make your own exceptions out of logic_error which will inherit something from logic_error.
As long as they inherit something from logic_error, its fine.
They just have to print something like "OverFlow Exception" and the state.

The book that I am using almost has no information about logic_error(Walter J. Savitch-Problem Solving with C++ 7th ed.)

By the way, you said they were runtime_errors but isn't it true that it was programmers logic error that cause the problem.
----------------------------------------------
For example

a=0,b=2,c=4

a=b-c

And a can't take negative numbers.
---------------------------------------------
Is this runtime or logic error?

To quote the standard, the exception hierarchy in stdexcept is:
exception
  logic_error
    domain_error
    invalid_argument
    length_error
    out_of_range
  runtime_error
    range_error
    overflow_error
    underflow_error


If you define a cs52::overflow_error based on std::logic_error, you're just gonna confuse yourself and others when it can't be caught from std::overflow_error.
Topic archived. No new replies allowed.