Jump to content

[C++] I don't understand this question. Why am I capturing an error with a class again? What exactly function does the class serve exactly?

Shir0u

I got this question on my C++ assignment and I am confused as to why am I capturing an error with a class. Here is the question:

2. Define a new exception class named “VarNameException” that must inherit from the C++ runtime_error class. This class will capture the error case for a variable name when it is empty, only blanks or starting with a number. For example, if the user tries to create with a variable name of “”, “ “, or “1name”, it will generate an exception and this class should capture the reason and the error value.

I want to know why VarNameException is made instead of just making and try ()throw and catch and have all the error cases distinguished at the catch step. Thank you.

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, Shir0u said:

I want to know why VarNameException is made instead of just making and try ()throw and catch and have all the error cases distinguished at the catch step. Thank you.

Boring answer: because the exercise is telling you to do so.

 

Sure there are always a dozen ways to do something. The point of exercises is to explicitely do things a certain way so you get a feel for how to do it and have seen the different ways you can do something.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

-> Moved to Programming

^^^^ That's my post ^^^^
<-- This is me --- That's your scrollbar -->
vvvv Who's there? vvvv

Link to comment
Share on other sites

Link to post
Share on other sites

19 hours ago, Shir0u said:

I want to know why VarNameException is made instead of just making and try ()throw and catch and have all the error cases distinguished at the catch step. Thank you.

Think about *how* different errors are distinguished at the "catch step". You differentiate by type:

try
{
	//Something...
}
catch (FileException& e)
{
	std::cout << "A operation on a file failed: " << e.what();
}
catch (LogicException& e)
{
 	std::cout << "A logic error occured: " << e.what(); 
}
catch (VarNameException& e)
{
 	std::cout << "A VarNameException occured: " << e.what(); 
}
catch (...)
{
 	//unknown exception 
}

Thus, in order to be able to catch a specific type of exception, you must create a new type for the exception. And a type in C++ is basically a class.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×