question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: Analyze @throws tags

See original GitHub issue

Description

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:open
  • Created 4 years ago
  • Reactions:31
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
kitsonkcommented, Jun 12, 2019

A potential compromise is for JS, that @throws is evaluated as a function that returns never of which, there is a level of checking built in to ensure that such a function doesn’t return a value.

2reactions
jhprattcommented, Jun 12, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found