Results 1 to 7 of 7

Thread: How to do error handling ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    76

    How to do error handling ?

    Hello. How to know the value of a variable i, when an error occurs ?

    Code:
    std::string line;
    unsigned long int i;	i=0;
    ...
    ...
    ...
    try
    {
    	for(i = 0; i < line.size(); i++)
    	{
    		if(isalpha(line[i]))
    		{
    				filename << line[i] << std::endl;
    		}
    	}// for
    
    catch( i ) 
    	{
    		cout << "error when i has the value =" << i << endl;
    	}
    }

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,000

    Re: How to do error handling ?

    Why do you expect an error from this code so that the catch() block is executed? Note that the closing } for the try { comes before the catch(), not after the catch block.

    The only possible exception that could be thrown is from the stream insertion. This would throw an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state. But from this code, member exceptions are not set to throw??

    Note in modern c++ this would be coded as

    Code:
    std::string line;
    ....
        for (const auto& l : line)
            if (isalpha(l))
                filename << l << std::endl;
    Last edited by 2kaud; Apr 29th, 2018 at 01:36 PM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    76

    Re: How to do error handling ?

    Thanks for your example.
    I'm just learning how to find and fix errors in a for loop.
    Code:
    try
    {
    	for(i = 0; i < line.size(); i++)
    	{
    		...For example.	Runtime Error has occurred somewhere here.
    		
    	}// for
    }
    catch( i ) 
    	{
    		cout << "error when i has the value =" << i << endl;
    	}

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: How to do error handling ?

    Quote Originally Posted by klen_ View Post
    Thanks for your example.
    I'm just learning how to find and fix errors in a for loop.
    Code:
    try
    {
    	for(i = 0; i < line.size(); i++)
    	{
    		...For example.	Runtime Error has occurred somewhere here.
    		
    	}// for
    }
    catch( i ) 
    	{
    		cout << "error when i has the value =" << i << endl;
    	}
    If there is an error you would need to catch whatever error is thrown, this is normally something derived from std::exception but it doesn't need to be. In your example i is just another variable with no special meaning to the compiler, however if the variable i is declared before the try block you should be able to access it in the catch block.

  5. #5
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,000

    Re: How to do error handling ?

    Quote Originally Posted by klen_ View Post
    Thanks for your example.
    I'm just learning how to find and fix errors in a for loop.
    Code:
    try
    {
    	for(i = 0; i < line.size(); i++)
    	{
    		...For example.	Runtime Error has occurred somewhere here.
    		
    	}// for
    }
    catch( i ) 
    	{
    		cout << "error when i has the value =" << i << endl;
    	}
    OK. When an error is detected for which an exception is to be created, then within a try block, thow() is used to 'throw' an exception which is 'caught' by a catch block. There can be several catch blocks - each with a different type of parameter. Which catch is caught from a throw depends upon the type thrown. For this example, consider

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	try {
    		for (int i = 0; i < 20; ++i)
    			if (i / 8)		// Error condition
    				throw i;
    			else
    				cout << i << " ";
    	}
    	catch (int e) {
    		cout << "\nError when i has the value " << e << endl;
    	}
    }
    In this case, the exception occurs when i becomes 8. The catch() catches all errors from the try block of type int. As the type used in a catch statement can be any type, it is common to create separate classes for each type of exception to be thrown. If you look at the documentation for various STL classes, you will see exceptions called out_of_range, invalid_argument etc.

    See http://www.learncpp.com/cpp-tutorial...tion-handling/

    These 'exception' classes are often based upon the base class exception. This base class basically has a constructor which takes an argument (const char * const) and a what() function which returns info about the exception. For the above test, this could be re-written using a new class based upon class exception. Consider

    Code:
    #include <iostream>
    #include <exception>
    #include <string>
    using namespace std;
    
    class Bad_For : public exception
    {
    public:
    	explicit Bad_For(int f_err) : exception(to_string(f_err).c_str()) {}
    };
    
    int main()
    {
    	try {
    		for (int i = 0; i < 20; ++i)
    			if (i / 8)		// Error condition
    				throw Bad_For(i);
    			else
    				cout << i << " ";
    	}
    
    	catch (const Bad_For& e) {
    		cout << "\nError when i has the value " << e.what() << endl;
    	}
    }
    Both these examples produce the display

    Code:
    0 1 2 3 4 5 6 7
    Error when i has the value 8
    Unless in very simple cases, the preferred way is as the second example and use a dedicated class (derived from exception).
    Last edited by 2kaud; May 1st, 2018 at 07:08 AM. Reason: Added example based upon exception
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    76

    Re: How to do error handling ?

    Apparently, all errors must be expected but I don't always know what will lead to errors.
    Thanks for your example.

  7. #7
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,000

    Re: How to do error handling ?

    If you don't know what errors to expect, then

    Code:
    catch (...)
    will catch all that haven't already been processed by previous catch statements. So from the above

    Code:
    catch (const Bad_For& e) {
        cout << "\nError when i has the value " << e.what() << endl;
    }
    catch (...) {
        cout << "Unknown error occured" << endl;
    }
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width