C/C++ Question in throwing a class object as exception in C++

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    C++ Class
Click For Summary
The discussion revolves around the implementation of exception handling in a C++ program that defines exception classes within a Rectangle class. It clarifies that throwing exceptions using constructors like `throw NegativeWidth(w)` indeed creates and throws an object of the respective exception class. The distinction between declaring an exception variable as `Rectangle::NegativeWidth e` versus using an instance of the Rectangle class is emphasized, noting that the former specifies the type of the exception, while the latter refers to an object. Additionally, the preference for defining exception classes as nested within another class versus as separate entities is debated, with opinions suggesting that nesting may not be necessary. Overall, the conversation highlights the nuances of exception handling and class design in C++.
  • #31
yungman said:
You read my respond? I COPIED STRAIGHT OUT OF THE CD GIVEN BY THE BOOK.

You've said this before and it was not true then. I don't think it's true now.

It's time to face an unfortunate and unpleasant fact: you are not a very good programmer. The reason you are not a very good programmer is twofold:
  1. You have clearly not understood the material in the earlier sections but have gone on to later sections. You have not grasped the concept of pointers (chapter 9), arrays (chapter 7), or variable typing (chapter 2).
  2. You ignore our attempts to help you. There are literally thousands of messages trying to help you and you make us repeat the same things many, many times. (see #15, #17, #19 and now #25 )
Thus far, you've blamed your book, me, your compilers, PF, your printer and your washing machine. None of them are at fault here. This is all of your doing.

You have a decision to make. You can try and become a better programmer which means changing your behavior, or you can keep doing what you're doing and remain poor at programming. Up to you.
 
Last edited:
  • Like
Likes Mark44, jbunniii and pbuk
Technology news on Phys.org
  • #32
yungman said:
No I am not, this is an exercise only, even if it were to declare double, I NEVER enter anything with decimals to test the program. Like 2, 3, -1, -3. The point is if the program say integer, just put integer.

I am very literal, if the book provide me the code, I take it literally and not wasting time question on whether it's int or double. My emphasis is on why the program using the class inside and how to interpreted the declaration.
No offense, but this attitude would get you nowhere in a professional or open-source project. A pull request where the author doesn't care about implicit narrowing of double to int because "I NEVER enter anything with decimals to test the program" is a pull request that's going to be declined by any competent software team.

Type safety (e.g. ensuring that the objects provided to a function are of the type expected by the function) is a hugely important theme in C++. Unfortunately, to maintain compatibility with C, it isn't enforced by the compiler for many legacy "plain old data" types such as double and int. This is a major source of bugs, which is why most compilers will emit a warning in this case. (If yours does not, then find the setting to enable warnings for implicit narrowing. Also, you should enable the setting to "treat warnings as errors" instead of ignoring or disabling warnings!)
 
Last edited:
  • Like
Likes pbuk, sysprog, Mark44 and 1 other person
  • #33
jbunniii said:
implicit narrowing

I agree implicit narrowing is a problem. But here I think that's not the problem.
  • If the variable will always hold an integer, it should be an int. Period. It should never be a double anywhere in the code.
  • If the variable will sometimes be fractional, it should never be an int. Period.
The problem is not implicit narrowing. The problem is a lack of clear thinking about what this variable is supposed to represent.Other problems flow from this.
 
  • Like
Likes sysprog
  • #34
Vanadium 50 said:
I agree implicit narrowing is a problem. But here I think that's not the problem.
  • If the variable will always hold an integer, it should be an int. Period. It should never be a double anywhere in the code.
  • If the variable will sometimes be fractional, it should never be an int. Period.
The problem is not implicit narrowing. The problem is a lack of clear thinking about what this variable is supposed to represent.Other problems flow from this.
Absolutely agree that this is the underlying problem. My suggestion about enabling the implicit narrowing warning is that it makes it much easier to recognize errors like this in the first place. If @yungman had enabled this warning along with "treat warnings as errors", he would have been aware of, and perhaps fixed, the problem before posting his code here. (Or, if not clear what the problem is and how to proceed, he could have asked about it here!)

This is even more important if he's using code from a CD and assuming that it's correct.
 
  • #35
jbunniii said:
If @yungman had enabled this warning along with "treat warnings as errors", he would have been aware of, and perhaps fixed, the problem before posting his code here.
Like myself, yungman is using Visual Studio, for which the default warning level is W3, one lower than the highest warning level (which treats warnings as errors). When I first ran the code I got the warning about conversion from double to int. I'm almost certain he got the same warning, assuming he compiled and ran the code.
 
Last edited:
  • Like
Likes jbunniii
  • #36
FWIW, this is Program 16-5 in Gaddis, which uses variable names that are un-yungmanized (e.g. myRectangle instead of myRect) and does not mix ints and doubles. So it is not the case that this is an exact copy. Would have saved some time.

I'm glad VS produces a warning for this. It would have been helpful for us to know that the code threw a warning. Would have saved some time.

But anyway...One thing Ada got right is that even Integers have ranges: you can't assign an Integer of one range to another that does not completely contain the first. One thing they got wrong was building in a default Integer type (and worse, with an implementation-dependent range). If you are going to have super-strong typing, have super-strong typing.

The advantage of this is that run-time errors in C/C++ become compile-time errors in Ada.
 
Last edited:
  • Like
Likes jbunniii

Similar threads

  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
10
Views
2K
Replies
89
Views
6K