1 //===--------------------- catch_pointer_nullptr.cpp ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++98, c++03, libcxxabi-no-exceptions 10 11 #include <cassert> 12 #include <cstdlib> 13 14 struct A {}; 15 16 void test1() 17 { 18 try 19 { 20 throw nullptr; 21 assert(false); 22 } 23 catch (int* p) 24 { 25 assert(!p); 26 } 27 catch (long*) 28 { 29 assert(false); 30 } 31 } 32 33 void test2() 34 { 35 try 36 { 37 throw nullptr; 38 assert(false); 39 } 40 catch (A* p) 41 { 42 assert(!p); 43 } 44 catch (int*) 45 { 46 assert(false); 47 } 48 } 49 50 template <class Catch> 51 void catch_nullptr_test() { 52 try { 53 throw nullptr; 54 assert(false); 55 } catch (Catch c) { 56 assert(!c); 57 } catch (...) { 58 assert(false); 59 } 60 } 61 62 63 int main() 64 { 65 // catch naked nullptrs 66 test1(); 67 test2(); 68 69 catch_nullptr_test<int*>(); 70 catch_nullptr_test<int**>(); 71 catch_nullptr_test<int A::*>(); 72 catch_nullptr_test<const int A::*>(); 73 catch_nullptr_test<int A::**>(); 74 } 75