Problem With Exception Specifications
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Problem With Exception Specifications

  1. #1
    Join Date
    Jun 2013
    Location
    Langrood, Iran
    Posts
    7

    Problem With Exception Specifications

    I'm Learning Exception Handling And now I Have a problem with Exception Specifications!

    The terminate( ) and unexpected( ) functions simply call other functions to actually handle
    an error. As just explained, by default terminate( ) calls abort( ), and unexpected( ) calls
    terminate( ).
    Right? But look at my code :

    Code:
    // Test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>     
    #include <stdexcept>
    #include <exception>
    using namespace std;
    
    void test(int a) throw()
    {
    	if(a != 0)
    		cout << "a = " << a << endl;
    
    	else
    		throw runtime_error("a == 0");
    }
    
    
    int main () 
    {
    
    	
    	int a;
    	while(cin >> a)
    	{
    		try
    		{
    			cout << "Test invoked!\n";
    			test(a);
    		}
    
    		catch(runtime_error &r)
    		{
    			cout << r.what();
    		}
    	}
    
    	return 0;
    }
    is Should terminate because of test function can't Throw!Am I Right?
    But it works correctly until user enters Ctrl+Z!

    even I try Some Codes From internet But they Don't Work properly either!
    like :

    Code:
    #include <iostream>       // std::cerr
    #include <exception>      // std::set_unexpected
    
    void myunexpected () {
      std::cerr << "unexpected called\n";
      throw 0;     // throws int (in exception-specification)
    }
    
    void myfunction () throw (int) {
      throw 'x';   // throws char (not in exception-specification)
    }
    
    int main (void) {
      std::set_unexpected (myunexpected);
      try {
        myfunction();
      }
      catch (int) { std::cerr << "caught int\n"; }
      catch (...) { std::cerr << "caught some other exception type\n"; }
      return 0;
    }
    it Should Print :
    unexpected called
    caught int
    But in My Compiler(VS 2010) output is :

    caught some other exception type
    I'm Confused!

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,993

    Re: Problem With Exception Specifications

    Your original code is working as expected. When a is 0, function test throws the runtime error "a == 0". This is caught by the catch in main and the message "a == 0" is printed. The while loop then continues. Your test function has a throw suffix but the compiler just generates a warning to the effect that test function assumed not to throw but does.

    In the second example, you are throwing a type char, but trying to catch a type int!Try adding this to main

    Code:
     catch (int) { std::cerr << "caught int\n"; }
     catch (char) { std::cerr << "caught char\n"; }
     catch (...) { std::cerr << "caught some other exception type\n"; }
    Again, the throw(int) function suffix for myfunction(..) just causes a compiler warning.

    Note that exception specifications have been deprecated in the new C++ standard.

    Here are two links explaining this:

    http://herbsutter.com/2010/03/13/tri...dards-meeting/
    http://www.gotw.ca/publications/mill22.htm

    Also note that for MSVC "The C++ Standard requires that unexpected is called when a function throws an exception that is not on its throw list. The current implementation does not support this." if you want to use unexpected(), then you need to explicitly call it yourself.

    http://msdn.microsoft.com/en-US/libr...=VS.80%29.aspx

    The following will print "unexpected called" and terminate.

    Code:
    #include <iostream>       // std::cerr
    #include <exception>      // std::set_unexpected
    
    void myunexpected () {
      std::cerr << "unexpected called\n";
      throw 0;     // throws int (in exception-specification)
    }
    
    void myfunction () throw (int) {
      throw 'x';   // throws char (not in exception-specification)
    }
    
    int main (void) {
      std::set_unexpected (myunexpected);
      try {
        myfunction();
      }
    
     catch (int) {
    	  std::cerr << "caught int\n"; 
      }
    
     /*catch (char) {
    	  std::cerr << "caught char\n"; 
      }*/
    
      catch (...) {
    	  unexpected();
    	  std::cerr << "caught some other exception type\n"; 
      }
      return 0;
    }
    Last edited by 2kaud; July 13th, 2013 at 10:05 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,694

    Re: Problem With Exception Specifications

    Herb Sutter has written about exception specifications here.

    His conclusions were...

    Moral #1: Never write an exception specification.

    Moral #2: Except possibly an empty one, but if I were you I’d avoid even that.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center