Skip to content

Commit

Permalink
STYLE: Catch exceptions by _const_ reference
Browse files Browse the repository at this point in the history
Replaced `catch \((\w+) &` with `catch (const $1 &`, using Visual Studio 2019,
Find and Replace, Replace in Files, Use regular expressions.

Following C++ Core Guidelines, April 10, 2022, which says that it is "typically
better" to catch by `const` reference than to catch by non-const reference. At
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#e15-throw-by-value-catch-exceptions-from-a-hierarchy-by-reference

With help from Lee Newberg.
  • Loading branch information
N-Dekker authored and hjmjohnson committed May 11, 2022
1 parent 46dd415 commit f222a5e
Show file tree
Hide file tree
Showing 88 changed files with 151 additions and 151 deletions.
6 changes: 3 additions & 3 deletions Modules/Core/Common/src/itkMultiThreaderBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,15 @@ MultiThreaderBase::SingleMethodProxy(void * arg)
(*workUnitInfoStruct->ThreadFunction)(arg);
workUnitInfoStruct->ThreadExitCode = WorkUnitInfo::ThreadExitCodeEnum::SUCCESS;
}
catch (ProcessAborted &)
catch (const ProcessAborted &)
{
workUnitInfoStruct->ThreadExitCode = WorkUnitInfo::ThreadExitCodeEnum::ITK_PROCESS_ABORTED_EXCEPTION;
}
catch (ExceptionObject &)
catch (const ExceptionObject &)
{
workUnitInfoStruct->ThreadExitCode = WorkUnitInfo::ThreadExitCodeEnum::ITK_EXCEPTION;
}
catch (std::exception &)
catch (const std::exception &)
{
workUnitInfoStruct->ThreadExitCode = WorkUnitInfo::ThreadExitCodeEnum::STD_EXCEPTION;
}
Expand Down
8 changes: 4 additions & 4 deletions Modules/Core/Common/src/itkPlatformMultiThreader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ PlatformMultiThreader::SingleMethodExecute()
process_id[thread_loop] = this->SpawnDispatchSingleMethodThread(&m_ThreadInfoArray[thread_loop]);
}
}
catch (std::exception & e)
catch (const std::exception & e)
{
// get the details of the exception to rethrow them
exceptionDetails = e.what();
Expand All @@ -163,7 +163,7 @@ PlatformMultiThreader::SingleMethodExecute()
m_ThreadInfoArray[0].NumberOfWorkUnits = m_NumberOfWorkUnits;
m_SingleMethod((void *)(&m_ThreadInfoArray[0]));
}
catch (ProcessAborted &)
catch (const ProcessAborted &)
{
// Need cleanup and rethrow ProcessAborted
// close down other threads
Expand All @@ -180,7 +180,7 @@ PlatformMultiThreader::SingleMethodExecute()
// rethrow
throw;
}
catch (std::exception & e)
catch (const std::exception & e)
{
// get the details of the exception to rethrow them
exceptionDetails = e.what();
Expand Down Expand Up @@ -208,7 +208,7 @@ PlatformMultiThreader::SingleMethodExecute()
exceptionOccurred = true;
}
}
catch (std::exception & e)
catch (const std::exception & e)
{
// get the details of the exception to rethrow them
exceptionDetails = e.what();
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/src/itkProcessObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ ProcessObject::UpdateOutputData(DataObject * itkNotUsed(output))
{
this->GenerateData();
}
catch (ProcessAborted &)
catch (const ProcessAborted &)
{
this->InvokeEvent(AbortEvent());
this->ResetPipeline();
Expand Down
10 changes: 5 additions & 5 deletions Modules/Core/Common/src/itkSmapsFileParser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ ITKCommon_EXPORT std::istream &
lastPos = in.tellg();
}
}
catch (ExceptionObject & excp)
catch (const ExceptionObject & excp)
{
record.Reset();
// propagate the exception
Expand Down Expand Up @@ -162,7 +162,7 @@ ITKCommon_EXPORT std::istream &
itkGenericExceptionMacro(<< "For record: " << record.m_RecordName << ", bad right bracket: " << bracket);
}
}
catch (ExceptionObject & excp)
catch (const ExceptionObject & excp)
{
record.Reset();
// propagate the exception
Expand Down Expand Up @@ -283,7 +283,7 @@ ITKCommon_EXPORT std::istream &
itkGenericExceptionMacro(<< "For record: " << record.m_RecordName << ", bad end of line: " << line);
}
}
catch (ExceptionObject & excp)
catch (const ExceptionObject & excp)
{
record.Reset();
// propagate the exception
Expand Down Expand Up @@ -405,7 +405,7 @@ operator>>(std::istream & smapsStream, SmapsData_2_6 & data)
record = new SmapsRecord;
}
}
catch (ExceptionObject & excp)
catch (const ExceptionObject & excp)
{
// in case of error, erase the records.
data.Reset();
Expand Down Expand Up @@ -524,7 +524,7 @@ operator>>(std::istream & stream, VMMapData_10_2 & data)
}
}
}
catch (ExceptionObject & excp)
catch (const ExceptionObject & excp)
{
// in case of error, erase the records.
data.Reset();
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/src/itkStreamingProcessObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ StreamingProcessObject::GenerateData()
this->StreamedGenerateData(piece);
this->UpdateProgress(static_cast<float>(piece + 1) / numberOfInputRequestRegion);
}
catch (ProcessAborted &)
catch (const ProcessAborted &)
{
this->InvokeEvent(AbortEvent());
this->ResetPipeline();
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/test/itkAbortProcessObjectTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ itkAbortProcessObjectTest(int, char *[])
{
extract->UpdateLargestPossibleRegion();
}
catch (itk::ProcessAborted &)
catch (const itk::ProcessAborted &)
{
if (onAbortCalled)
{
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/test/itkAnnulusOperatorTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ itkAnnulusOperatorTest(int, char *[])
std::cout << e;
return EXIT_FAILURE;
}
catch (std::exception & e)
catch (const std::exception & e)
{
std::cout << "Std exception" << e.what();
return EXIT_FAILURE;
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/test/itkExceptionObjectTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ itkExceptionObjectTest(int, char *[])
// the OneShouldFail variable is never actually set to false!
OneShouldFail &= (hal == john); // ERROR
}
catch (itk::IncompatibleOperandsError & e)
catch (const itk::IncompatibleOperandsError & e)
{
std::cout << e << std::endl;
raised = true;
Expand Down Expand Up @@ -170,7 +170,7 @@ itkExceptionObjectTest(int, char *[])
E.SetDescription("sample error");
throw E;
}
catch (itk::ExceptionObject &e) { std::cout << e << std::endl; }
catch (const itk::ExceptionObject &e) { std::cout << e << std::endl; }
*/

delete Fp;
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/test/itkFilterDispatchTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ itkFilterDispatchTest(int, char *[])
std::cout << "Executing 5-d filter: ";
filter5d->Update();
}
catch (std::string & err)
catch (const std::string & err)
{
std::cout << err;
passed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ itkImageSpatialObjectTest(int, char *[])
{
imageSO->ValueAtInWorldSpace(q, returnedValue);
}
catch (itk::ExceptionObject &)
catch (const itk::ExceptionObject &)
{
throw;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,11 +523,11 @@ MRIBiasFieldCorrectionFilter<TInputImage, TOutputImage, TMaskImage>::EstimateBia
optimizer->StartOptimization();
}
}
catch (ExceptionObject & ie)
catch (const ExceptionObject & ie)
{
std::cerr << ie << std::endl;
}
catch (std::exception & e)
catch (const std::exception & e)
{
std::cerr << e.what() << std::endl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ GaussianExponentialDiffeomorphicTransform<TParametersValueType, VDimension>::Gau
{
smoother->Update();
}
catch (ExceptionObject & exc)
catch (const ExceptionObject & exc)
{
std::string msg("Caught exception: ");
msg += exc.what();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ GaussianSmoothingOnUpdateDisplacementFieldTransform<TParametersValueType, VDimen
{
smoother->Update();
}
catch (ExceptionObject & exc)
catch (const ExceptionObject & exc)
{
std::string msg("Caught exception: ");
msg += exc.what();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ FFTWComplexToComplex1DFFTImageFilter<TInputImage, TOutputImage>::BeforeThreadedG
m_InputBufferArray[i] = new typename FFTW1DProxyType::ComplexType[lineSize];
m_OutputBufferArray[i] = new typename FFTW1DProxyType::ComplexType[lineSize];
}
catch (std::bad_alloc &)
catch (const std::bad_alloc &)
{
itkExceptionMacro("Problem allocating memory for internal computations");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ FFTWForward1DFFTImageFilter<TInputImage, TOutputImage>::BeforeThreadedGenerateDa
m_InputBufferArray[i] = new typename FFTW1DProxyType::ComplexType[lineSize];
m_OutputBufferArray[i] = new typename FFTW1DProxyType::ComplexType[lineSize];
}
catch (std::bad_alloc &)
catch (const std::bad_alloc &)
{
itkExceptionMacro("Problem allocating memory for internal computations");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ FFTWInverse1DFFTImageFilter<TInputImage, TOutputImage>::BeforeThreadedGenerateDa
m_InputBufferArray[i] = new typename FFTW1DProxyType::ComplexType[lineSize];
m_OutputBufferArray[i] = new typename FFTW1DProxyType::ComplexType[lineSize];
}
catch (std::bad_alloc &)
catch (const std::bad_alloc &)
{
itkExceptionMacro("Problem allocating memory for internal computations");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ FastMarchingBase<TInput, TOutput>::GenerateData()
}
}
}
catch (ProcessAborted &)
catch (const ProcessAborted &)
{
// User aborted filter execution Here we catch an exception thrown by the
// progress reporter and rethrow it with the correct line number and file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ FastMarchingImageFilterBase<TInput, TOutput>::InitializeOutput(OutputImageType *
{
relabeler->Update();
}
catch (ExceptionObject & excep)
catch (const ExceptionObject & excep)
{
std::cout << excep << std::endl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ FastMarchingUpwindGradientImageFilter<TLevelSet, TSpeedImage>::GenerateData()
{
Superclass::GenerateData();
}
catch (ProcessAborted & exc)
catch (const ProcessAborted & exc)
{
// process was aborted, clean up the state of the filter
// (most of the cleanup will have already been done by the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ itkJoinSeriesImageFilterTest(int, char *[])
{
joinSeriesImage->Update();
}
catch (itk::InvalidRequestedRegionError & err)
catch (const itk::InvalidRequestedRegionError & err)
{
std::cout << err << std::endl;
passed = true;
Expand Down
2 changes: 1 addition & 1 deletion Modules/Filtering/ImageGrid/test/itkShrinkImageTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ itkShrinkImageTest(int, char *[])
// this should fail due to a bad requested region
shrink->Update();
}
catch (itk::InvalidRequestedRegionError & e)
catch (const itk::InvalidRequestedRegionError & e)
{
std::cout << e << std::endl;
std::cout << std::endl << std::endl << "Exception caught, updating largest possible region instead." << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ itkHistogramMatchingImageFilterTest()
<< "ERROR: Reached code that should have aborted due to thrown exception of missing ReferenceHistogram\n"
<< __FILE__ << ":" << __LINE__ << std::endl;
}
catch (itk::ExceptionObject &)
catch (const itk::ExceptionObject &)
{
std::cout << "Test caught known exception for SetReferenceHistogram correctly, NO FAILURE!" << std::endl;
}
Expand All @@ -391,7 +391,7 @@ itkHistogramMatchingImageFilterTest()
std::cout << "ERROR: Reached code that should have aborted due to thrown exception of missing ReferenceImage\n"
<< __FILE__ << ":" << __LINE__ << std::endl;
}
catch (itk::ExceptionObject &)
catch (const itk::ExceptionObject &)
{
std::cout << "Test caught known exception for SetReferenceImage correctly, NO FAILURE!" << std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/BMP/src/itkBMPImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ BMPImageIO::CanReadFile(const char * filename)
{
this->OpenFileForReading(inputStream, fname);
}
catch (ExceptionObject &)
catch (const ExceptionObject &)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/BioRad/src/itkBioRadImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ BioRadImageIO::CanReadFile(const char * filename)
{
this->OpenFileForReading(file, fname);
}
catch (ExceptionObject &)
catch (const ExceptionObject &)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/DCMTK/src/itkDCMTKImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ DCMTKImageIO::CanReadFile(const char * filename)
{
this->OpenFileForReading(file, filename);
}
catch (ExceptionObject &)
catch (const ExceptionObject &)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/GDCM/src/itkGDCMImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ GDCMImageIO::CanReadFile(const char * filename)
{
this->OpenFileForReading(file, filename);
}
catch (ExceptionObject &)
catch (const ExceptionObject &)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/GE/src/itkGE4ImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ GE4ImageIO::CanReadFile(const char * FileNameToRead)
{
this->OpenFileForReading(f, FileNameToRead);
}
catch (ExceptionObject &)
catch (const ExceptionObject &)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/GE/src/itkGE5ImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ GE5ImageIO ::CheckGE5xImages(char const * const imageFileTemplate, std::string &
{
this->OpenFileForReading(f, imageFileTemplate);
}
catch (ExceptionObject &)
catch (const ExceptionObject &)
{
reason = "File could not be opened for read";
return -1;
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/GE/src/itkGEAdwImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ GEAdwImageIO::CanReadFile(const char * FileNameToRead)
{
this->OpenFileForReading(f, FileNameToRead);
}
catch (ExceptionObject &)
catch (const ExceptionObject &)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/GIPL/src/itkGiplImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ GiplImageIO::CanReadFile(const char * filename)
{
this->OpenFileForReading(inputStream, filename);
}
catch (ExceptionObject &)
catch (const ExceptionObject &)
{
return false;
}
Expand Down
Loading

0 comments on commit f222a5e

Please sign in to comment.