Using try catch vs do while loops in C++

  • C/C++
  • Thread starter CFDFEAGURU
  • Start date
  • Tags
    C++ Loops
In summary, user input errors are anything *but* exceptional. They are in fact quite common and the normal solution is to reprompt the user for another input, remaining within the main line code to handle that input. Thanks for your input Jeff Reid it is greatly appreciated.
  • #1
CFDFEAGURU
783
10
Hello all,

First let me state the following:

1. The programs that I write are not production code.
2. I am not a trained developer.
3. I do not work for a software development company.

My question pertains to the use of try catch exception handling vs. using do while loops.

Currently, I screen all user input in the following manner.

Below is an example of a simple user input function named, tube_od(), this function simply asks the user for the tube od in inches, converts the od to meters. If the tube od has been entered as a zero or negative value the output to the user is "Invalid Entry" and the question is repeated until the proper conditions are met.

Code:
void tube_od(){
// Obtain the tube outer diameter in inches
	do{
		printf("Tube OD (inches) ");
		getline(cin,s);
		XDO = string_number<double>(s);
		XDO = XDO*0.0254;	// Convert inches to meters.
		if(XDO <= 0) cout << "Invalid Entry" << endl;
	}while(XDO <= 0);
}

Here is the function string_number()

Code:
template <typename T>
// Templated function used to obtain the correct user
// input. All user input comes in as a text string and
// then is converted by this function to the appropriate
// typedef.
  T string_number (const string &Text)
  {
     istringstream ss(Text);
     T result;
     return ss >> result ? result : -1;
  }

The above method works fine but I was wondering if it would be better to use a try catch statement and take advantage of the C++ exception and error handling.

I would appreciate any comments.

Thanks
Matt
 
Technology news on Phys.org
  • #2
I normally use Try/Catch for things similar to what you are doing but to also include the cases where the user enters things like strings instead of the expected numbers. Type casting can yield sometimes unpredictable results that can throw errors, or worse not throw an exception but give you a garbage output. You can define your own exceptions as well if you want to be more specific than just what the compiler throws at you. I would use a try/catch block anytime you are working with user input.
 
  • #3
Experience shows that attempting to write exception-safe code where the try{} block includes casts between strings and number types is insane without using one of the boost casts. There simply isn't enough contained within the standard library to do such things safely.

If you find yourself needing to convert between types in this way, boost::lexical_cast provides everything you need - safely!
 
  • #4
You should (usually) reserve exceptions for exceptional cases (errors) rather than regular program logic.
 
  • #5
You should (usually) reserve exceptions for exceptional cases (errors) rather than regular program logic.

That is the rule that I follow.

I was just curious to know what some other programmers thoughts were on the subjcect of exceptions.

Experience shows that attempting to write exception-safe code where the try{} block includes casts between strings and number types is insane without using one of the boost casts. There simply isn't enough contained within the standard library to do such things safely.

If you find yourself needing to convert between types in this way, boost::lexical_cast provides everything you need - safely!

Thanks! That I didn't know.

Thanks
Matt
 
  • #6
CFDFEAGURU said:
I was just curious to know what some other programmers thoughts were on the subjcect of exceptions.
My opinion: User input errors are anything *but* exceptional. They are in fact quite common.
 
  • #7
In the case of user input errors, the normal solution is to reprompt the user for another input, remaining within the main line code to handle that input.

In the case of a "real" error, the program can't continue normally and so exception handling makes more sense.

In some cases, a program selects a method based on what is available on a system. For example a program could test to see if direct X version 10 was installed, and if not, check to see if version 9 (or lower) was available and setup for that. In my case, I generally deal with stuff like this by using pointer to functions (or overriding member functions) so that the main line code remains the same and the differences are dealt with inside the functions being called.
 
  • #8
In the case of user input errors, the normal solution is to reprompt the user for another input, remaining within the main line code to handle that input.

That is exactly what my programs will do when the user input is not within the parameters that I have specified for that input.

In the case of a "real" error, the program can't continue normally and so exception handling makes more sense.

I agree and currently that is what my programs will do.

Thanks for you input Jeff Reid it is greatly appreciated.

Matt
 

1. What is the difference between using a try catch and a do while loop in C++?

Try catch and do while loops are two different forms of control structures in C++ that serve different purposes. A try catch block is used to handle potential errors or exceptions that may occur in a program, while a do while loop is used to repeat a block of code until a certain condition is met.

2. When should I use a try catch block instead of a do while loop?

A try catch block should be used when there is a possibility of an error or exception occurring in a program, such as user input, file operations, or network connections. It allows for the program to handle these errors gracefully and prevent the program from crashing. A do while loop, on the other hand, should be used when a block of code needs to be repeated until a specific condition is met, such as prompting a user for input until a valid response is given.

3. Can I use a try catch and a do while loop together in C++?

Yes, it is possible to use a try catch block within a do while loop or vice versa. This can be useful in situations where a program needs to repeat a block of code until a condition is met, but also needs to handle any errors that may occur during the execution of that code.

4. Is one method more efficient than the other?

Efficiency depends on the specific situation and purpose of the code. A try catch block may add some overhead to the program, but it is necessary for handling potential errors. A do while loop may also be more efficient in certain situations where the code needs to be repeated multiple times.

5. Are there any alternatives to using try catch and do while loops in C++?

Yes, there are other control structures and error handling techniques in C++ such as if statements, for loops, and throwing and catching specific exceptions. The choice of which method to use depends on the specific needs and requirements of the program.

Similar threads

Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
4
Views
11K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Replies
15
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
16K
Back
Top