Skip to content

Commit

Permalink
STYLE: Rethrow caught exceptions just by throw; without argument
Browse files Browse the repository at this point in the history
Following C++ Core Guidelines, April 10, 2022, which says:

> To rethrow a caught exception use `throw;` not `throw e;`

At https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#e15-throw-by-value-catch-exceptions-from-a-hierarchy-by-reference

Found by the regular expression, `catch \(const [^\s]+ & (\w+)\).+  throw \1;`
  • Loading branch information
N-Dekker authored and dzenanz committed Jul 29, 2022
1 parent 8f5c337 commit 15fb8f2
Show file tree
Hide file tree
Showing 19 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ FastMarchingUpwindGradientImageFilter<TLevelSet, TSpeedImage>::GenerateData()
{
Superclass::GenerateData();
}
catch (const ProcessAborted & exc)
catch (const ProcessAborted &)
{
// process was aborted, clean up the state of the filter
// (most of the cleanup will have already been done by the
// superclass)

// restore the original stopping value
this->SetStoppingValue(stoppingValue);
throw exc;
throw;
}

// restore the original stopping value
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/CSV/include/itkCSVNumericObjectFileWriter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ CSVNumericObjectFileWriter<TValue, VRows, VColumns>::Write()
std::cerr << "Exception caught! " << std::endl;
std::cerr << exp << std::endl;
outputStream.close();
throw exp;
throw;
}
catch (...)
{
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/ImageBase/include/itkIOTestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class IOTestHelper
{
std::cout << "Caught an exception: " << std::endl;
std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;
throw err;
throw;
}
catch (...)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ GradientDescentOptimizer::ResumeOptimization()
{
m_CostFunction->GetValueAndDerivative(this->GetCurrentPosition(), m_Value, m_Gradient);
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
// An exception has occurred.
// Terminate immediately.
Expand All @@ -97,7 +97,7 @@ GradientDescentOptimizer::ResumeOptimization()
StopOptimization();

// Pass exception to caller
throw err;
throw;
}

if (m_Stop)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ RegularStepGradientDescentBaseOptimizer::ResumeOptimization()
m_StopConditionDescription << "Cost function error after " << m_CurrentIteration << " iterations. "
<< excp.GetDescription();
this->StopOptimization();
throw excp;
throw;
}

if (m_Stop)
Expand Down
4 changes: 2 additions & 2 deletions Modules/Numerics/Optimizers/src/itkSPSAOptimizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ SPSAOptimizer::AdvanceOneStep()
{
this->ComputeGradient(currentPosition, m_Gradient);
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
// An exception has occurred.
// Terminate immediately.
m_StopCondition = StopConditionSPSAOptimizerEnum::MetricError;
StopOptimization();
// Pass exception to caller
throw err;
throw;
}

/** Compute the gain a_k */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ ConjugateGradientLineSearchOptimizerv4Template<TInternalComputationValueType>::A
/* Pass gradient to transform and let it do its own updating. */
this->m_Metric->UpdateTransformParameters(this->m_Gradient);
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
this->m_StopCondition = StopConditionObjectToObjectOptimizerEnum::UPDATE_PARAMETERS_ERROR;
this->m_StopConditionDescription << "UpdateTransformParameters error";
this->StopOptimization();
// Pass exception to caller
throw err;
throw;
}

this->InvokeEvent(IterationEvent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ GradientDescentLineSearchOptimizerv4Template<TInternalComputationValueType>::Adv
/* Pass gradient to transform and let it do its own updating */
this->m_Metric->UpdateTransformParameters(this->m_Gradient);
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
this->m_StopCondition = StopConditionObjectToObjectOptimizerEnum::UPDATE_PARAMETERS_ERROR;
this->m_StopConditionDescription << "UpdateTransformParameters error";
this->StopOptimization();
// Pass exception to caller
throw err;
throw;
}
this->InvokeEvent(IterationEvent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ GradientDescentOptimizerv4Template<TInternalComputationValueType>::ResumeOptimiz
// proper size, no new allocation is done.
this->m_Metric->GetValueAndDerivative(this->m_CurrentMetricValue, this->m_Gradient);
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
this->m_StopCondition = StopConditionObjectToObjectOptimizerEnum::COSTFUNCTION_ERROR;
this->m_StopConditionDescription << "Metric error during optimization";
this->StopOptimization();

// Pass exception to caller
throw err;
throw;
}

// Check if optimization has been stopped externally.
Expand Down Expand Up @@ -175,14 +175,14 @@ GradientDescentOptimizerv4Template<TInternalComputationValueType>::AdvanceOneSte
// Pass gradient to transform and let it do its own updating
this->m_Metric->UpdateTransformParameters(this->m_Gradient);
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
this->m_StopCondition = StopConditionObjectToObjectOptimizerEnum::UPDATE_PARAMETERS_ERROR;
this->m_StopConditionDescription << "UpdateTransformParameters error";
this->StopOptimization();

// Pass exception to caller
throw err;
throw;
}

this->InvokeEvent(IterationEvent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ MultiGradientOptimizerv4Template<TInternalComputationValueType>::ResumeOptimizat
itkDebugMacro(" combine-grad ");
this->m_OptimizersList[0]->GetModifiableMetric()->UpdateTransformParameters(this->m_Gradient);
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
this->m_StopCondition = StopConditionObjectToObjectOptimizerEnum::UPDATE_PARAMETERS_ERROR;
this->m_StopConditionDescription << "UpdateTransformParameters error";
this->StopOptimization();
// Pass exception to caller
throw err;
throw;
}
this->InvokeEvent(IterationEvent());
/* Update and check iteration count */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ QuasiNewtonOptimizerv4Template<TInternalComputationValueType>::AdvanceOneStep()
/* Pass gradient to transform and let it do its own updating */
this->m_Metric->UpdateTransformParameters(this->m_NewtonStep);
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
this->m_StopCondition = StopConditionObjectToObjectOptimizerEnum::UPDATE_PARAMETERS_ERROR;
this->m_StopConditionDescription << "UpdateTransformParameters error";
this->StopOptimization();

// Pass exception to caller
throw err;
throw;
}

this->InvokeEvent(IterationEvent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ RegularStepGradientDescentOptimizerv4<TInternalComputationValueType>::AdvanceOne
// Pass gradient to transform and let it do its own updating
this->m_Metric->UpdateTransformParameters(this->m_Gradient, factor);
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
this->m_StopCondition = StopConditionObjectToObjectOptimizerEnum::UPDATE_PARAMETERS_ERROR;
this->m_StopConditionDescription << "UpdateTransformParameters error";
this->StopOptimization();

// Pass exception to caller
throw err;
throw;
}

this->InvokeEvent(IterationEvent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ ImageRegistrationMethod<TFixedImage, TMovingImage>::StartOptimization()
// do the optimization
m_Optimizer->StartOptimization();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
// An error has occurred in the optimization.
// Update the parameters
m_LastTransformParameters = m_Optimizer->GetCurrentPosition();

// Pass exception to caller
throw err;
throw;
}

// get the results
Expand Down Expand Up @@ -266,12 +266,12 @@ ImageRegistrationMethod<TFixedImage, TMovingImage>::GenerateData()
// initialize the interconnects between components
this->Initialize();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
m_LastTransformParameters = empty;

// pass exception to caller
throw err;
throw;
}

this->StartOptimization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,24 @@ ImageToSpatialObjectRegistrationMethod<TFixedImage, TMovingSpatialObject>::Gener
// Initialize the interconnects between components
this->Initialize();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
// Pass exception to caller
throw err;
throw;
}

try
{
// Do the optimization
m_Optimizer->StartOptimization();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
// An error has occurred in the optimization.
// Update the parameters
m_LastTransformParameters = m_Optimizer->GetCurrentPosition();
// Pass exception to caller
throw err;
throw;
}

// Get the results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,28 +350,28 @@ MultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::GenerateData(
// initialize the interconnects between components
this->Initialize();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
m_LastTransformParameters = ParametersType(1);
m_LastTransformParameters.Fill(0.0f);

// pass exception to caller
throw err;
throw;
}

try
{
// do the optimization
m_Optimizer->StartOptimization();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
// An error has occurred in the optimization.
// Update the parameters
m_LastTransformParameters = m_Optimizer->GetCurrentPosition();

// Pass exception to caller
throw err;
throw;
}

// get the results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,28 @@ PointSetToImageRegistrationMethod<TFixedPointSet, TMovingImage>::GenerateData()
{
this->Initialize();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
m_LastTransformParameters = ParametersType(1);
m_LastTransformParameters.Fill(0.0f);

// Pass the exception to the caller
throw err;
throw;
}

// Do the optimization
try
{
m_Optimizer->StartOptimization();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
// An error has occurred in the optimization.
// Update the parameters
m_LastTransformParameters = m_Optimizer->GetCurrentPosition();

// Pass the exception to the caller
throw err;
throw;
}

// Get the results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,28 @@ PointSetToPointSetRegistrationMethod<TFixedPointSet, TMovingPointSet>::GenerateD
{
this->Initialize();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
m_LastTransformParameters = ParametersType(1);
m_LastTransformParameters.Fill(0.0f);

// Pass the exception to the caller
throw err;
throw;
}

// Do the optimization
try
{
m_Optimizer->StartOptimization();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
// An error has occurred in the optimization.
// Update the parameters
m_LastTransformParameters = m_Optimizer->GetCurrentPosition();

// Pass the exception to the caller
throw err;
throw;
}

// Get the results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ BayesianClassifierInitializationImageFilter<TInputImage, TProbabilityPrecisionTy
{
kmeansFilter->Update();
}
catch (const ExceptionObject & err)
catch (const ExceptionObject &)
{
// Pass exception to caller
throw err;
throw;
}

typename KMeansFilterType::ParametersType estimatedMeans = kmeansFilter->GetFinalMeans(); // mean of each class
Expand Down
4 changes: 2 additions & 2 deletions Modules/Video/Core/src/itkTemporalProcessObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,12 @@ TemporalProcessObject::UpdateOutputData(DataObject * itkNotUsed(output))
}
this->GenerateData();
}
catch (const ProcessAborted & excp)
catch (const ProcessAborted &)
{
this->InvokeEvent(AbortEvent());
this->ResetPipeline();
this->RestoreInputReleaseDataFlags();
throw excp;
throw;
}
catch (...)
{
Expand Down

0 comments on commit 15fb8f2

Please sign in to comment.