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

  • C/C++
  • Thread starter yungman
  • Start date
  • Tags
    C++ Class
In summary: The called method will now do its thing and return. The object itself does not store the method. So if you say myRect.setWidth(5), the Rectangle object myRect will call the method setWidth. The object itself does not need to have a stored version of setWidth, it just needs to know where to look for the method. This is why you can call methods on objects without the compiler complaining that there's no method that exists for the object. The compiler knows where to look for the methods and how to call them, so it's happy. Now if you say Rectangle::NegativeLength e, you are not calling a method, but you are accessing the class table of Rectangle and inside
  • #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
<h2> What is a class object in C++?</h2><p>A class object in C++ is an instance of a class, which is a user-defined data type that encapsulates data and functions. It can be thought of as a blueprint for creating objects with specific attributes and behaviors.</p><h2> How do you throw a class object as an exception in C++?</h2><p>To throw a class object as an exception in C++, you can use the <code>throw</code> keyword followed by the object you want to throw. This will send an exception of that object's type to the nearest <code>catch</code> block in the program.</p><h2> Why would you throw a class object as an exception in C++?</h2><p>Throwing a class object as an exception in C++ allows you to handle different types of errors or exceptional situations in your program. By creating custom class objects to represent specific exceptions, you can provide more detailed information about the error and how to handle it.</p><h2> Can you catch a class object as an exception in C++?</h2><p>Yes, you can catch a class object as an exception in C++. When an exception is thrown, the program will search for a <code>catch</code> block that matches the type of the thrown object. If a match is found, the code inside that <code>catch</code> block will be executed.</p><h2> How can you handle a thrown class object exception in C++?</h2><p>To handle a thrown class object exception in C++, you can use a <code>try-catch</code> block. The code that may throw an exception is placed inside the <code>try</code> block, and the corresponding <code>catch</code> block will handle the exception if it occurs. You can also use multiple <code>catch</code> blocks to handle different types of exceptions.</p>

FAQ: Question in throwing a class object as exception in C++

What is a class object in C++?

A class object in C++ is an instance of a class, which is a user-defined data type that encapsulates data and functions. It can be thought of as a blueprint for creating objects with specific attributes and behaviors.

How do you throw a class object as an exception in C++?

To throw a class object as an exception in C++, you can use the throw keyword followed by the object you want to throw. This will send an exception of that object's type to the nearest catch block in the program.

Why would you throw a class object as an exception in C++?

Throwing a class object as an exception in C++ allows you to handle different types of errors or exceptional situations in your program. By creating custom class objects to represent specific exceptions, you can provide more detailed information about the error and how to handle it.

Can you catch a class object as an exception in C++?

Yes, you can catch a class object as an exception in C++. When an exception is thrown, the program will search for a catch block that matches the type of the thrown object. If a match is found, the code inside that catch block will be executed.

How can you handle a thrown class object exception in C++?

To handle a thrown class object exception in C++, you can use a try-catch block. The code that may throw an exception is placed inside the try block, and the corresponding catch block will handle the exception if it occurs. You can also use multiple catch blocks to handle different types of exceptions.

Similar threads

Replies
19
Views
1K
Replies
17
Views
2K
Replies
36
Views
4K
Replies
31
Views
2K
Replies
23
Views
2K
Replies
39
Views
4K
Replies
5
Views
2K
Replies
36
Views
2K
Back
Top