[Bug c++/58796] New: throw nullptr not caught by catch(type*)

seurer at us dot ibm.com gcc-bugzilla@gcc.gnu.org
Fri Oct 18 20:03:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58796

            Bug ID: 58796
           Summary: throw nullptr not caught by catch(type*)
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: seurer at us dot ibm.com

C++ standard section 15.3
A handler is a match for an exception object of type E if
...
— the handler is of type cv T or const T& where T is a pointer or pointer to
member type and E is std::nullptr_t.

A catch handler of a pointer type (the "T" in the quote from the standard)
should catch a throw of nullptr (the E). This is different than, say, "throw 7"
which will only be caught by something like an int catch (7 being an int) and
not a long catch.

However, when you try this with gcc it doesn't work.  Only a catch with
nullptr_t will catch throws of nullptr; catches with other pointer types that
should catch it don't.  

Note that this may instead be an issue with the library.

This is the output I get from the program that follows:

About to 'throw nullptr' (first case)
Didn't catch 'throw nullptr' at all
About to 'throw nullptr' (second case)
Didn't catch 'throw nullptr' at all
About to 'throw nullptr' (last case)
Caught 'throw nullptr' as type 'std::nullptr_t'


#include <iostream>
#include <cstddef>

struct A {};
typedef int A::*PointerToMember;

int main() {
   try {
      try {
         std::cout << "About to 'throw nullptr' (first case)" << std::endl;
         throw nullptr;
      } catch (PointerToMember) { // This *should* catch throw nullptr...
         std::cout << "Caught 'throw nullptr' as type 'int
A::*PointerToMember'" << std::endl;
      }
   } catch (...) {
      std::cout << "Didn't catch 'throw nullptr' at all" << std::endl;
   }

   try {
      try {
         std::cout << "About to 'throw nullptr' (second case)" << std::endl;
         throw nullptr;
      } catch(void *) { // This *should* catch throw nullptr...
         std::cout << "Caught 'throw nullptr' as type 'void *'" << std::endl;
      }
   } catch (...) {
      std::cout << "Didn't catch 'throw nullptr' at all" << std::endl;
   }

   try {
      try {
         std::cout << "About to 'throw nullptr' (last case)" << std::endl;
         throw nullptr;
      } catch (PointerToMember) {
         std::cout << "Caught 'throw nullptr' as type 'int
A::*PointerToMember'" << std::endl;
      } catch(void *) {
         std::cout << "Caught 'throw nullptr' as type 'void *'" << std::endl;
      } catch(std::nullptr_t) {
         std::cout << "Caught 'throw nullptr' as type 'std::nullptr_t'" <<
std::endl;
      }
   } catch (...) {
      std::cout << "Didn't catch 'throw nullptr' at all" << std::endl;
   }

   return 0;
}


More information about the Gcc-bugs mailing list