1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #include "pxr/pxr.h"
25 #include "pxr/base/tf/exception.h"
26 #include "pxr/base/tf/diagnostic.h"
27 #include "pxr/base/tf/regTest.h"
28 
29 #include <string>
30 
31 PXR_NAMESPACE_USING_DIRECTIVE
32 
33 class Tf_TestException : public TfBaseException
34 {
35 public:
36     using TfBaseException::TfBaseException;
37     virtual ~Tf_TestException();
38 };
39 
~Tf_TestException()40 Tf_TestException::~Tf_TestException()
41 {
42 }
43 
44 static bool
Test_TfException()45 Test_TfException()
46 {
47     try {
48         TF_THROW(Tf_TestException, "test exception 1");
49     }
50     catch (TfBaseException const &exc) {
51         TF_AXIOM(std::string(exc.what()) == std::string("test exception 1"));
52         TF_AXIOM(exc.GetThrowContext());
53     }
54     catch (...) {
55         TF_FATAL_ERROR("Expected exception was not thrown");
56     }
57 
58     try {
59         TF_THROW(Tf_TestException, TfSkipCallerFrames(2), "test exception 2");
60     }
61     catch (TfBaseException const &exc) {
62         TF_AXIOM(std::string(exc.what()) == std::string("test exception 2"));
63         TF_AXIOM(exc.GetThrowContext());
64     }
65     catch (...) {
66         TF_FATAL_ERROR("Expected exception was not thrown");
67     }
68 
69     return true;
70 }
71 
72 TF_ADD_REGTEST(TfException);
73