Avoid unncecessary try except in Python
When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception.Bad:```pythontry: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data)Install this rule for wispbit Cloud
Add this rule to wispbit and it will run when you open a pull request
Install this rule with wispbit CLI
Run this command in your terminal to install the rule locally
Install this rule for Coderabbit
Copy the configuration below and add it to your repository as .coderabbit.yml in your project root.
reviews: path_instructions: - path: "*.py" instructions: | When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception. Bad: ```python try: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data) except (NetworkError, DBError): logger.error("Operation failed") ``` Bad: ```python try: # Contains only one potential error but still # has a block of code unrelated to the exception url = "https://google.com" url += "/?search=hello" response = requests.get(url) data = response.json() print(data) except NetworkError as e: logger.error(f"Error: {e}") ``` Bad: ```python # Try except blocks are nested into each other try: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}") except requests.RequestException as e: logger.error(f"Error: {e}") ``` Good: ```python try: # Only one function that could have an error user_data = get_user_data() except NetworkError: logger.error("Failed to fetch user data") return # Cannot raise an exception so it doesn't need to be handled process_data(user_data) try: # Only one potential error save_to_db(user_data) except DBError: logger.error("Failed to save to database") return ``` Good: ```python url = "https://google.com" url += "/?search=hello" # Network call is a separate try except block try: response = requests.get(url) response.raise_for_status() except RequestException as e: logger.error(f"Error: {e}") # Getting response in json is a separate try except block try: data = response.json() except JSONDecodeError as e: logger.error(f"Error: {e}") ``` Good: ```python # Blocks that were nested before are now unnested # into separate blocks try: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) except requests.RequestException as e: logger.error(f"Error: {e}") try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}") ``` Install this rule for Greptile
Greptile rules can be added through the web interface. Please see this documentation for details on how to add custom rules and context.
When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception.Bad:```pythontry: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data)except (NetworkError, DBError): logger.error("Operation failed")```Bad:```pythontry: # Contains only one potential error but still # has a block of code unrelated to the exception url = "https://google.com" url += "/?search=hello" response = requests.get(url) data = response.json() print(data)except NetworkError as e: logger.error(f"Error: {e}")```Bad:```python# Try except blocks are nested into each othertry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")except requests.RequestException as e: logger.error(f"Error: {e}")```Good:```pythontry: # Only one function that could have an error user_data = get_user_data()except NetworkError: logger.error("Failed to fetch user data") return# Cannot raise an exception so it doesn't need to be handledprocess_data(user_data)try: # Only one potential error save_to_db(user_data)except DBError: logger.error("Failed to save to database") return```Good:```pythonurl = "https://google.com"url += "/?search=hello"# Network call is a separate try except blocktry: response = requests.get(url) response.raise_for_status()except RequestException as e: logger.error(f"Error: {e}")# Getting response in json is a separate try except blocktry: data = response.json()except JSONDecodeError as e: logger.error(f"Error: {e}")```Good:```python# Blocks that were nested before are now unnested# into separate blockstry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], )except requests.RequestException as e: logger.error(f"Error: {e}")try: json.loads(response.choices[0].message.parsed)except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")```File Path Patterns:
Install this rule for GitHub Copilot
Copilot instructions can be added through the interface. See the documentation for details on how to create coding guidelines.
When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception.Bad:```pythontry: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data)except (NetworkError, DBError): logger.error("Operation failed")```Bad:```pythontry: # Contains only one potential error but still # has a block of code unrelated to the exception url = "https://google.com" url += "/?search=hello" response = requests.get(url) data = response.json() print(data)except NetworkError as e: logger.error(f"Error: {e}")```Bad:```python# Try except blocks are nested into each othertry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")except requests.RequestException as e: logger.error(f"Error: {e}")```Good:```pythontry: # Only one function that could have an error user_data = get_user_data()except NetworkError: logger.error("Failed to fetch user data") return# Cannot raise an exception so it doesn't need to be handledprocess_data(user_data)try: # Only one potential error save_to_db(user_data)except DBError: logger.error("Failed to save to database") return```Good:```pythonurl = "https://google.com"url += "/?search=hello"# Network call is a separate try except blocktry: response = requests.get(url) response.raise_for_status()except RequestException as e: logger.error(f"Error: {e}")# Getting response in json is a separate try except blocktry: data = response.json()except JSONDecodeError as e: logger.error(f"Error: {e}")```Good:```python# Blocks that were nested before are now unnested# into separate blockstry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], )except requests.RequestException as e: logger.error(f"Error: {e}")try: json.loads(response.choices[0].message.parsed)except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")```File Path Patterns:
Install this rule for Graphite Diamond
Diamond custom rules can be added through the interface. See the documentation for details on how to create custom rules.
When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception.Bad:```pythontry: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data)except (NetworkError, DBError): logger.error("Operation failed")```Bad:```pythontry: # Contains only one potential error but still # has a block of code unrelated to the exception url = "https://google.com" url += "/?search=hello" response = requests.get(url) data = response.json() print(data)except NetworkError as e: logger.error(f"Error: {e}")```Bad:```python# Try except blocks are nested into each othertry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")except requests.RequestException as e: logger.error(f"Error: {e}")```Good:```pythontry: # Only one function that could have an error user_data = get_user_data()except NetworkError: logger.error("Failed to fetch user data") return# Cannot raise an exception so it doesn't need to be handledprocess_data(user_data)try: # Only one potential error save_to_db(user_data)except DBError: logger.error("Failed to save to database") return```Good:```pythonurl = "https://google.com"url += "/?search=hello"# Network call is a separate try except blocktry: response = requests.get(url) response.raise_for_status()except RequestException as e: logger.error(f"Error: {e}")# Getting response in json is a separate try except blocktry: data = response.json()except JSONDecodeError as e: logger.error(f"Error: {e}")```Good:```python# Blocks that were nested before are now unnested# into separate blockstry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], )except requests.RequestException as e: logger.error(f"Error: {e}")try: json.loads(response.choices[0].message.parsed)except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")```File Path Patterns:
Use with Cline
Copy the rule below and ask Cline to review your code using this rule
When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception.Bad:```pythontry: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data)except (NetworkError, DBError): logger.error("Operation failed")```Bad:```pythontry: # Contains only one potential error but still # has a block of code unrelated to the exception url = "https://google.com" url += "/?search=hello" response = requests.get(url) data = response.json() print(data)except NetworkError as e: logger.error(f"Error: {e}")```Bad:```python# Try except blocks are nested into each othertry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")except requests.RequestException as e: logger.error(f"Error: {e}")```Good:```pythontry: # Only one function that could have an error user_data = get_user_data()except NetworkError: logger.error("Failed to fetch user data") return# Cannot raise an exception so it doesn't need to be handledprocess_data(user_data)try: # Only one potential error save_to_db(user_data)except DBError: logger.error("Failed to save to database") return```Good:```pythonurl = "https://google.com"url += "/?search=hello"# Network call is a separate try except blocktry: response = requests.get(url) response.raise_for_status()except RequestException as e: logger.error(f"Error: {e}")# Getting response in json is a separate try except blocktry: data = response.json()except JSONDecodeError as e: logger.error(f"Error: {e}")```Good:```python# Blocks that were nested before are now unnested# into separate blockstry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], )except requests.RequestException as e: logger.error(f"Error: {e}")try: json.loads(response.choices[0].message.parsed)except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")```Use with OpenAI Codex
Copy the rule below and ask OpenAI Codex to review your code using this rule
When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception.Bad:```pythontry: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data)except (NetworkError, DBError): logger.error("Operation failed")```Bad:```pythontry: # Contains only one potential error but still # has a block of code unrelated to the exception url = "https://google.com" url += "/?search=hello" response = requests.get(url) data = response.json() print(data)except NetworkError as e: logger.error(f"Error: {e}")```Bad:```python# Try except blocks are nested into each othertry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")except requests.RequestException as e: logger.error(f"Error: {e}")```Good:```pythontry: # Only one function that could have an error user_data = get_user_data()except NetworkError: logger.error("Failed to fetch user data") return# Cannot raise an exception so it doesn't need to be handledprocess_data(user_data)try: # Only one potential error save_to_db(user_data)except DBError: logger.error("Failed to save to database") return```Good:```pythonurl = "https://google.com"url += "/?search=hello"# Network call is a separate try except blocktry: response = requests.get(url) response.raise_for_status()except RequestException as e: logger.error(f"Error: {e}")# Getting response in json is a separate try except blocktry: data = response.json()except JSONDecodeError as e: logger.error(f"Error: {e}")```Good:```python# Blocks that were nested before are now unnested# into separate blockstry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], )except requests.RequestException as e: logger.error(f"Error: {e}")try: json.loads(response.choices[0].message.parsed)except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")```Use with Cursor
Copy the rule below and ask Cursor to review your code using this rule
When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception.Bad:```pythontry: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data)except (NetworkError, DBError): logger.error("Operation failed")```Bad:```pythontry: # Contains only one potential error but still # has a block of code unrelated to the exception url = "https://google.com" url += "/?search=hello" response = requests.get(url) data = response.json() print(data)except NetworkError as e: logger.error(f"Error: {e}")```Bad:```python# Try except blocks are nested into each othertry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")except requests.RequestException as e: logger.error(f"Error: {e}")```Good:```pythontry: # Only one function that could have an error user_data = get_user_data()except NetworkError: logger.error("Failed to fetch user data") return# Cannot raise an exception so it doesn't need to be handledprocess_data(user_data)try: # Only one potential error save_to_db(user_data)except DBError: logger.error("Failed to save to database") return```Good:```pythonurl = "https://google.com"url += "/?search=hello"# Network call is a separate try except blocktry: response = requests.get(url) response.raise_for_status()except RequestException as e: logger.error(f"Error: {e}")# Getting response in json is a separate try except blocktry: data = response.json()except JSONDecodeError as e: logger.error(f"Error: {e}")```Good:```python# Blocks that were nested before are now unnested# into separate blockstry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], )except requests.RequestException as e: logger.error(f"Error: {e}")try: json.loads(response.choices[0].message.parsed)except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")```Use with Claude Code
Copy the rule below and ask Claude Code to review your code using this rule
When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception.Bad:```pythontry: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data)except (NetworkError, DBError): logger.error("Operation failed")```Bad:```pythontry: # Contains only one potential error but still # has a block of code unrelated to the exception url = "https://google.com" url += "/?search=hello" response = requests.get(url) data = response.json() print(data)except NetworkError as e: logger.error(f"Error: {e}")```Bad:```python# Try except blocks are nested into each othertry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")except requests.RequestException as e: logger.error(f"Error: {e}")```Good:```pythontry: # Only one function that could have an error user_data = get_user_data()except NetworkError: logger.error("Failed to fetch user data") return# Cannot raise an exception so it doesn't need to be handledprocess_data(user_data)try: # Only one potential error save_to_db(user_data)except DBError: logger.error("Failed to save to database") return```Good:```pythonurl = "https://google.com"url += "/?search=hello"# Network call is a separate try except blocktry: response = requests.get(url) response.raise_for_status()except RequestException as e: logger.error(f"Error: {e}")# Getting response in json is a separate try except blocktry: data = response.json()except JSONDecodeError as e: logger.error(f"Error: {e}")```Good:```python# Blocks that were nested before are now unnested# into separate blockstry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], )except requests.RequestException as e: logger.error(f"Error: {e}")try: json.loads(response.choices[0].message.parsed)except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")```Install this rule for Windsurf
To set up rules for Windsurf Reviews, please see this documentation
When using try-except blocks in Python, keep the try block focused only on the code that can raise the expected exception.Bad:```pythontry: # Large block of code with many potential errors user_data = get_user_data() process_data(user_data) save_to_db(user_data)except (NetworkError, DBError): logger.error("Operation failed")```Bad:```pythontry: # Contains only one potential error but still # has a block of code unrelated to the exception url = "https://google.com" url += "/?search=hello" response = requests.get(url) data = response.json() print(data)except NetworkError as e: logger.error(f"Error: {e}")```Bad:```python# Try except blocks are nested into each othertry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], ) try: json.loads(response.choices[0].message.parsed) except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")except requests.RequestException as e: logger.error(f"Error: {e}")```Good:```pythontry: # Only one function that could have an error user_data = get_user_data()except NetworkError: logger.error("Failed to fetch user data") return# Cannot raise an exception so it doesn't need to be handledprocess_data(user_data)try: # Only one potential error save_to_db(user_data)except DBError: logger.error("Failed to save to database") return```Good:```pythonurl = "https://google.com"url += "/?search=hello"# Network call is a separate try except blocktry: response = requests.get(url) response.raise_for_status()except RequestException as e: logger.error(f"Error: {e}")# Getting response in json is a separate try except blocktry: data = response.json()except JSONDecodeError as e: logger.error(f"Error: {e}")```Good:```python# Blocks that were nested before are now unnested# into separate blockstry: response = client.beta.chat.completions.parse( model="some-model", messages=[ {"role": "system", "content": "hello"}, {"role": "user", "content": "how are you"}, ], )except requests.RequestException as e: logger.error(f"Error: {e}")try: json.loads(response.choices[0].message.parsed)except json.JSONDecodeError as e: logger.error(f"Decode failed: {e}")```