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.

Forbid `raise Exception`

See original GitHub issue

Rule request

Thesis

Forbid to raise Exception or BaseException

# bad
raise Exception
raise Exception("user not found")
raise BaseException
raise BaseException("user not found")

# good
raise UserNotFoundError
raise UserNotFoundError("cannot find user with the given id")

Reasoning

Bare exception hard to catch and when you catch them you can suppress something else.

On Built-in Exceptions:

The built-in exceptions listed below can be generated by the interpreter or built-in functions…

User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition “just like” the situation in which the interpreter raises the same exception; but beware that there is nothing to prevent user code from raising an inappropriate error.

On User-defined Exceptions:

When creating a module that can raise several distinct errors, a common practice is to create a base class for exceptions defined by that module, and subclass that to create specific exception classes for different error conditions.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
hhsdevcommented, Oct 7, 2020

@orsinium @sobolevn Shouldn’t this be closed since the PR is merged?

0reactions
hhsdevcommented, Sep 3, 2020

@sobolevn Ah my bad, didn’t read the code thoroughly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I raise a Response Forbidden in django
if you want to raise an exception you can use: from django.core.exceptions import PermissionDenied def your_view(.
Read more >
Built-in Exceptions — Python 3.11.1 documentation
User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition “just like”...
Read more >
Python werkzeug.exceptions.Forbidden() Examples
This page shows Python examples of werkzeug.exceptions.Forbidden. ... def post(self, name): if not may(DELETE): raise Forbidden() try: with ...
Read more >
How to Throw Exceptions in Node.js - Rollbar
Node.js Throw Exception; How do you protect your application against exceptions? Wrapping Up; Track, Analyze and Manage Errors With Rollbar.
Read more >
How to throw the http403 forbidden error programmaticaly ...
User-1087479560 posted. Hi, Please try this: protected void Page_Load(object sender, EventArgs e) { Response.StatusCode = 403; Response.Flush(); Response.
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