It depends on whether you can deal with the exceptions that can be raised at this point or not.
If you can handle the exceptions locally you should, and it is better to handle the error as close to where it is raised as possible.
If you can't handle them locally then just having a try / finally block is perfectly reasonable - assuming there's some code you need to execute regardless of whether the method succeeded or not. For example (from Neil's comment), opening a stream and then passing that stream to an inner method to be loaded is an excellent example of when you'd need try { } finally { }, using the finally clause to ensure that the stream is closed regardless of the success or failure of the read.
However, you will still need an exception handler somewhere in your code - unless you want your application to crash completely of course. It depends on the architecture of your application exactly where that handler is.
Answer from ChrisF on Stack Exchangelanguage agnostic - Why use try … finally without a catch clause? - Software Engineering Stack Exchange
try without catch/finally is not documented
java - Does it make sense to do "try-finally" without "catch"? - Stack Overflow
IL2CPP Windows - Try block ends without any catch, finally, nor fault handler - Unity 2019.4.35 - 2019.4.37
It depends on whether you can deal with the exceptions that can be raised at this point or not.
If you can handle the exceptions locally you should, and it is better to handle the error as close to where it is raised as possible.
If you can't handle them locally then just having a try / finally block is perfectly reasonable - assuming there's some code you need to execute regardless of whether the method succeeded or not. For example (from Neil's comment), opening a stream and then passing that stream to an inner method to be loaded is an excellent example of when you'd need try { } finally { }, using the finally clause to ensure that the stream is closed regardless of the success or failure of the read.
However, you will still need an exception handler somewhere in your code - unless you want your application to crash completely of course. It depends on the architecture of your application exactly where that handler is.
The finally block is used for code that must always run, whether an error condition (exception) occurred or not.
The code in the finally block is run after the try block completes and, if a caught exception occurred, after the corresponding catch block completes. It is always run, even if an uncaught exception occurred in the try or catch block.
The finally block is typically used for closing files, network connections, etc. that were opened in the try block. The reason is that the file or network connection must be closed, whether the operation using that file or network connection succeeded or whether it failed.
Care should be taken in the finally block to ensure that it does not itself throw an exception. For example, be doubly sure to check all variables for null, etc.
This is useful if you want the currently executing method to still throw the exception while allowing resources to be cleaned up appropriately. Below is a concrete example of handling the exception from a calling method.
public void yourOtherMethod() {
try {
yourMethod();
} catch (YourException ex) {
// handle exception
}
}
public void yourMethod() throws YourException {
try {
db.store(mydata);
} finally {
db.cleanup();
}
}
It's there because the programmer wanted to make sure that db.cleanup() is called even if the code inside the try block throws an exception. Any exceptions will not be handled by that block, but they'll only be propagated upwards after the finally block is executed. The finally block will also be executed if there was no exception.
I keep receiving this error: 'try' without 'catch', 'finally' or resource declarations. I’ve tried to add and remove curly brackets, add final blocks, and catch blocks and nothing is working.
https://codepen.io/angelineb/pen/KKQpbqV
finally should have atleast a try block, catch is optional. The point of finally blocks is to make sure stuff gets cleaned up whether an exception is thrown or not. As per the JLS
A finally clause ensures that the finally block is executed after the try block and any catch block that might be executed, no matter how control leaves the try block or catch block.
Hence a finally should always be preceded by a try block.
You must have a try block with a finally block. The try block defines which lines of code will be followed by the finally code. If an exception is thrown prior to the try block, the finally code will not execute.
Adding catch blocks is optional:
try {
// something
} finally {
// guaranteed to run if execution enters the try block
}
I disagree, if you cannot do anything about an exception being thrown, but something further up your caller hierarchy can, then use the finally to clean up your resources and let the caller deal with cleaning up after the exception is thrown.
I do not agree.
try{}finally{} should be used in cases where you cannot handle the exception, but are required to clean up resources.
A try{}finally{} block will not cause the exception to "disappear" as you seem to think it will. It will be thrown up the stack and be handled somewhere else. If you are unable to see the exception in your current application it's because it's being thrown away elsewhere.
try {
connection = createConnection();
}
finally {
closeConnection(connection) //Free database connection.
}
In this case, you may not have any ability to handle an SQL exception, but you still want to free the database connection.
This is how I checked for the 403 error:
from github import GithubException
result = []
organization = g.get_organization("org_name")
invites = organization.invitations()
try:
#get invites from result
for invite in invites:
print(result.append(invite))
except GithubException as e:
if e.status== 403:
print("You need admin access")
Reference: GithubException Class
Note: e.data gives the actual exception data.
Update: There are specific exceptions to raise which are shown here
This open-source repository has many pre-build Github actions made using PyGithub that we can use directly for any automations. (Mentioning this as I found it to be really cool)
They are creating an open-source framework for writing Runbooks using Jupyter notebooks.
There are several ways of catching exceptions. I will make just two examples below. Please read more on how to properly use the try/except block. The problem with your code is that GithubException, as you typed it, is just a variable name, probably not even defined.
You can try:
for commit in commits:
try:
...
except Exception as e:
print(f'got Exception {e}')
continue
The block below will catch any exception and continue.
You can be more sophisticated and check the traceback to continue only if a particular error message is given.
import traceback
for commit in commits:
try:
...
except Exception as e:
tb = traceback.format_exc()
if 'Git Repository is empty' in tb:
print(f'The repo is empty!')
continue
else:
...