0

I'm not sure the question is appropriate but I'll try my best.

This is my homework problem. The homework ask me to throw an exception if two lines are parallel or equal.

The original codes are provided by my professor and my job is to modify it to make it able to throw an exception.

line.h

class RuntimeException{
 private:
 string errorMsg;
 public:
 RuntimeException(const string& err) { errorMsg = err; }
 string getMessage() const { return errorMsg; }
};

class EqualLines: public RuntimeException{
 public:
 //empty
};

class ParallelLines: public RuntimeException{
 public:
 //empty
};

class Line{
 public:
 Line(double slope, double y_intercept): a(slope), b(y_intercept) {};
 double intersect(const Line L) const throw(ParallelLines,
                    EqualLines);
 //...getter and setter
 private:
 double a;
 double b;
};

Professor told us NOT to modify the header file, only the .cpp file can be modifed.

line.cpp

double Line::intersect(const Line L) const throw(ParallelLines,
                       EqualLines){
 //below is my own code
 if ((getSlope() == L.getSlope()) && (getIntercept() != L.getIntercept())) {
 //then it is parallel, throw an exception
 }
 else if ((getSlope() == L.getSlope()) && (getIntercept() ==  L.getIntercept())) {
 //then it is equal, throw an exception
 }
 else {
    //return x coordinate of that point
    return ((L.getIntercept()-getIntercept()) / (getSlope()-L.getSlope()));
 }
 //above is my own code
}

since those two inherited classes are empty hence no constructor to initialize the errorMsg , nor I can create an object of those classes to throw exception. Any alternate solution to achieve this?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Travis Su
  • 670
  • 2
  • 7
  • 17
  • 2
    The code you posted uses the old `throw` specifications. Consider dropping those and using the new and modern `noexcept` specifier if the function doesn't throw anything, and nothing otherwise. In your case, please inform your professor to not use this old technique any more – Rakete1111 Jan 16 '17 at 19:27
  • 2
    *Professor told us NOT to modify the header file, only the .cpp file can be modifed.* -- Your professor should know that user-defined exceptions should be derived from `std::exception`, i.e. `class RuntimeException : public std::exception {...};` – PaulMcKenzie Jan 16 '17 at 19:39
  • @PaulMcKenzie Why should they derive from `std::exception`? – David G Jan 30 '17 at 17:55
  • @0x499602D2 [See this](http://stackoverflow.com/questions/1669514/should-i-inherit-from-stdexception) – PaulMcKenzie Jan 30 '17 at 19:07

1 Answers1

5

Because you have an exception specifier, you may only throw EqualLines or ParallelLines. These exception types have no default constructor (their base type's doesn't have a default constructor) and have no other constructors. The only way to construct either of these exceptions is to copy an existing one. It's impossible to throw either of those exceptions without modifying the headers or violating the standard. I would consult with the professor, it looks like a mistake to me.

In general, exception specifiers are a bad idea. See this answer. They are in fact deprecated.

Community
  • 1
  • 1
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • many of my classmates already consulted with the professor and the TA, but here's what they told us so far. – Travis Su Jan 16 '17 at 20:20
  • 1
    His assignment is defective. You have to choose to either break the assignment's rules or the language's rules. – François Andrieux Jan 16 '17 at 20:22
  • Here's what TA say: "Since frequently asked, here is the hint that I can provide: Again, the two inherited classes EqualLines and ParallelLines are derived from the superclass RuntimeException. As Dr. Tindell made a comment on the code, all the member variables and member functions (including the constructor) you need are inherited from the superclass RuntimeException, because the body of both EqualLines and ParallelLines are leaved blank. In other words, whatever the functionalities the superclass has, these two inherited classes have them all." – Travis Su Jan 16 '17 at 20:23
  • language's rules? – Travis Su Jan 16 '17 at 20:23
  • 1
    @TravisSu Constructors are not inherited. – François Andrieux Jan 16 '17 at 20:23
  • 1
    @TravisSu If you don't have to obey the standard (the "rules of the language") then you can make anything appear to work. For example : `throw reinterpret_cast(RuntimeException("Oops"));` will appear to *work* but it's undefined behavior. – François Andrieux Jan 16 '17 at 20:28
  • tried, but the program failed to catch. Didn't output anything. – Travis Su Jan 16 '17 at 20:33
  • 1
    @TravisSu That's the problem with undefined behavior, it's not defined. It can appear to work sometimes, it can crash other times, it can do anything else. – François Andrieux Jan 16 '17 at 20:35
  • @TravisSu *EqualLines and ParallelLines are derived from the superclass* -- Just the term `superclass` hints of a Java programmer trying to teach C++. C++ programmers use terms such as "base class" and "derived class". Having Java programmers teach C++, and use Java as a model in teaching C++ is a horrible idea. – PaulMcKenzie Jan 17 '17 at 02:04
  • @TravisSu Now looking at this, I am practically convinced this is a Java assignment trying to be retrofitted for C++. In Java, you have this `throws xxxException` syntax (which is deprecated in C++ and should be **well known** to be deprecated by someone teaching C++, thus not waste time teaching it). Also, the erroneous information given to you concerning the rules of C++ and inheriting constructors, not following the standard way of creating C++ exceptions by deriving from `std::exception`, etc.etc. Looks like you and your fellow students are being cheated. – PaulMcKenzie Jan 17 '17 at 02:22
  • For what it's worth, I agree with Paul that this seems like the most likely scenario. – François Andrieux Jan 17 '17 at 02:26
  • Professor extended the deadline but still didn't give any explanation about it, I'll just stick with reinterpret_cast for now and see what he will say on tomorrow's class.. – Travis Su Jan 19 '17 at 03:38
  • @PaulMcKenzie Some features were deprecated for no good reason. Deprecation isn't a reason to ignore a useful feature (like static). But uselessness (plus deprecation) certainly is. – curiousguy Jan 25 '17 at 20:20
  • @TravisSu I would be interested in knowing what the expected solution was. – François Andrieux Jan 26 '17 at 20:11
  • @FrançoisAndrieux lol the professor agreed us to modify header file..... – Travis Su Jan 30 '17 at 17:34