Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Add logic to this catch clause or eliminate it and rethrow the exception automatically.
-
Default Severity:Minor
-
Impact:Low
-
Likelihood:Low
-
Default Quality Profiles:Sonar way
-
Targeted languages:ABAP, Flex
-
Covered Languages:C#, C, C++, Java, JavaScript, Objective-C, PHP, PL/SQL, Python, Swift, T-SQL, TypeScript, VB.Net
-
Irrelevant for Languages:Cobol, Go, HTML, PL/I, RPG, VB6, XML
-
Remediation Function:Constant/Issue
-
Constant Cost:5min
-
Analysis Level:Syntactic Analysis
-
Analysis Scope:Main Sources
-
Common Rule:Yes
-
ReSharper:RedundantCatchClause
-
TSLint-SonarTS:no-useless-catch
-
Quick-fixes:
Description
A catch clause that only rethrows the caught exception has the same effect as omitting the catch altogether and letting it bubble up automatically, but with more code and the additional detriment of leaving maintainers scratching their heads.
Such clauses should either be eliminated or populated with the appropriate logic.
Noncompliant Code Example
string s = ""; try { s = File.ReadAllText(fileName); } catch (Exception e) // Noncompliant { throw; }
Compliant Solution
string s = ""; try { s = File.ReadAllText(fileName); } catch (Exception e) // Compliant { logger.LogError(e); throw; }
or
string s = File.ReadAllText(fileName);
Attachments
Issue Links
- is duplicated by
-
RSPEC-1164 Exceptions should not be caught and immediately rethrown
- Closed
- is implemented by
-
SONARABAP-359 Rule: "catch" clauses should do more than rethrow
-
- Open
-
-
SONARFLEX-129 Rule: "catch" clauses should do more than rethrow
-
- Open
-
-
SONARSLANG-65 Rule S2737: "catch" clauses should do more than rethrow
-
- Open
-
-
CPP-1545 Rule: "catch" clauses should do more than rethrow
-
- Closed
-
-
SONARPHP-748 Rule S2737: "catch" clauses should do more than rethrow
-
- Closed
-
-
SONARPLSQL-683 Rule S2737: "EXCEPTION WHEN ... THEN" clauses should do more than "RAISE"
-
- Closed
-
-
SONARPY-275 Rule S2737: "catch" clauses should do more than rethrow
-
- Closed
-
-
SONARSWIFT-358 Rule: "catch" clauses should do more than rethrow
-
- Closed
-
-
SONARTSQL-114 Rule: "CATCH" clauses should do more than rethrow
-
- Closed
-
-
SONARVBSIX-265 Rule: "catch" clauses should do more than rethrow
-
- Closed
-
-
SONARJAVA-1472 Rule 2737: "catch" clauses should do more than rethrow
-
- Closed
-
- links to
1.
|
Java | RSPEC-3496 |
|
Active | Unassigned | |
2.
|
C-Family | RSPEC-3684 |
|
Active | Unassigned | |
3.
|
PHP | RSPEC-4336 |
|
Active | Unassigned | |
4.
|
T-SQL: "CATCH" clauses should do more than rethrow | RSPEC-4509 |
|
Active | Unassigned | |
5.
|
Swift | RSPEC-4613 |
|
Active | Unassigned | |
6.
|
VB.NET | RSPEC-4858 |
|
Active | Unassigned | |
7.
|
JavaScript | RSPEC-4862 |
|
Active | Unassigned | |
8.
|
C# | RSPEC-4866 |
|
Active | Unassigned | |
9.
|
PL/SQL: "EXCEPTION WHEN ... THEN" clauses should do more than "RAISE" | RSPEC-5215 |
|
Active | Unassigned | |
10.
|
Python: "except" clauses should do more than raise the same issue | RSPEC-5588 |
|
Active | Unassigned |