Skip to content

Commit b1c947c

Browse files
committed
BUG: Simplify exception handling in itkPointSetToListSampleAdaptorTest
- Replace repetitive try-catch blocks with `ITK_TRY_EXPECT_EXCEPTION` macros for improved clarity and maintainability.
1 parent 9b00710 commit b1c947c

1 file changed

Lines changed: 6 additions & 66 deletions

File tree

Modules/Numerics/Statistics/test/itkPointSetToListSampleAdaptorTest.cxx

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <fstream>
2121

2222
#include "itkPointSetToListSampleAdaptor.h"
23+
#include "itkTestingMacros.h"
2324

2425
int
2526
itkPointSetToListSampleAdaptorTest(int, char *[])
@@ -41,73 +42,12 @@ itkPointSetToListSampleAdaptorTest(int, char *[])
4142

4243
auto listSample = PointSetToListSampleAdaptorType::New();
4344

44-
bool exceptionsProperlyCaught = true;
4545
// Test if the methods throw exceptions if invoked before setting the pointset
46-
try
47-
{
48-
// Purposely calling the Size() method in order to trigger an exception.
49-
listSample->Size();
50-
std::cerr << "Exception should have been thrown since the input point set is not set yet" << std::endl;
51-
exceptionsProperlyCaught = false;
52-
}
53-
catch (const itk::ExceptionObject & excp)
54-
{
55-
std::cerr << "Caught expected exception: " << excp << std::endl;
56-
}
57-
try
58-
{
59-
// Purposely calling the GetTotalFrequency() method in order to trigger an exception.
60-
listSample->GetTotalFrequency();
61-
std::cerr << "Exception should have been thrown since the input point set is not set yet" << std::endl;
62-
exceptionsProperlyCaught = false;
63-
}
64-
catch (const itk::ExceptionObject & excp)
65-
{
66-
std::cerr << "Caught expected exception: " << excp << std::endl;
67-
}
68-
69-
try
70-
{
71-
const PointSetToListSampleAdaptorType::MeasurementVectorType m = listSample->GetMeasurementVector(0);
72-
std::cerr << "Exception should have been thrown since the input point set is not set yet" << std::endl;
73-
std::cerr << "The invalid listSample->GetMeasurementVector is: " << m << std::endl;
74-
exceptionsProperlyCaught = false;
75-
}
76-
catch (const itk::ExceptionObject & excp)
77-
{
78-
std::cerr << "Caught expected exception: " << excp << std::endl;
79-
}
80-
81-
try
82-
{
83-
// Purposely calling the GetPointSet() method in order to trigger an exception.
84-
listSample->GetPointSet();
85-
std::cerr << "Exception should have been thrown since the input point set is not set yet" << std::endl;
86-
exceptionsProperlyCaught = false;
87-
}
88-
catch (const itk::ExceptionObject & excp)
89-
{
90-
std::cerr << "Caught expected exception: " << excp << std::endl;
91-
}
92-
93-
try
94-
{
95-
// Purposely calling the GetFrequency() method in order to trigger an exception.
96-
listSample->GetFrequency(0);
97-
std::cerr << "Exception should have been thrown since the input point set is not set yet" << std::endl;
98-
exceptionsProperlyCaught = false;
99-
}
100-
catch (const itk::ExceptionObject & excp)
101-
{
102-
std::cerr << "Caught expected exception: " << excp << std::endl;
103-
}
104-
105-
if (!exceptionsProperlyCaught)
106-
{
107-
std::cerr << "At least one exception that should have been caught was not." << std::endl;
108-
return EXIT_FAILURE;
109-
}
110-
46+
ITK_TRY_EXPECT_EXCEPTION(listSample->Size());
47+
ITK_TRY_EXPECT_EXCEPTION(listSample->GetTotalFrequency());
48+
ITK_TRY_EXPECT_EXCEPTION(listSample->GetMeasurementVector(0));
49+
ITK_TRY_EXPECT_EXCEPTION(listSample->GetPointSet());
50+
ITK_TRY_EXPECT_EXCEPTION(listSample->GetFrequency(0));
11151

11252
listSample->SetPointSet(pointSet);
11353

0 commit comments

Comments
 (0)