Feature: Analyze @throws tags
See original GitHub issueDescription
Given a function has a @throws
JSDoc tag, it would be helpful to apply some analysis and raise a compiler warning (or, if configured, an error) when the caller does not handle the error and it doesn’t declare its own @throws
tag.
Examples
/**
* @throws {SomeException}
*/
function myFunction() {
// some logic
// then for some reason we throw an exception
if (somethingWentWrong) {
throw new SomeException("something happened");
}
// some more logic
}
function potentialMess() {
myFunction(); // Compiler error: 'myFunction' may throw `SomeException`. ts(9876)
}
/**
* @throws {SomeException}
*/
function letItBubble() {
myFunction(); // No compiler errors
}
function aFunctionThatHandlesTheException() {
try {
myFunction(); // No compiler errors
}
catch (error) {
// do something with the error
}
}
Questions
-
Ideally, it would be even better if VSCode realizes that
myFunction
throws an error, and then@throws
is not required for the analysis. Still, this approach would be useful when dealing with external code/interfaces. -
It is my understanding that JSDoc’s
@throws
is allowed once – if this is correct, then VSCode could ignore this and accept multiple tags anyway? Otherwise, maybe we can use a different tag, e.g.@exception
?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:31
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Discuss/clarify the use of @throws tag in interfaces ... - Drupal
The @throws tag can have three purposes: 1. Inform about possible exceptions thrown directly by the same function/method.
Read more >Code Inspection: Redundant @throws tag(s) - JetBrains
Reports @throws tags for exceptions that are not thrown by the function or method. The exception classes added to the Unchecked Exceptions ...
Read more >Add support for @throws · Issue #73623 · microsoft/vscode
Given a function has a @throws JSDoc tag, it would be helpful to apply some analysis and raise a compiler warning (or, if...
Read more >How to use the Throws keyword in Java (and when ... - Rollbar
The throws keyword is used to declare which exceptions can be thrown from a method, while the throw keyword is used to explicitly...
Read more >5 Ways Post Tagging Can Revolutionize Your Social ... - Rival IQ
Select only the tags you want to compare for fully customizable analysis. To do this, I set up an Auto-Tag Rule on anything...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A potential compromise is for JS, that
@throws
is evaluated as a function that returnsnever
of which, there is a level of checking built in to ensure that such a function doesn’t return a value.For what it’s worth, I’d very much like some sort of indicator that a function can throw. I just tried extracting out a common “unexpected state” throw, and realized that I can’t do so safely because TS lacks this functionality.