Why is try {...} finally {...} good; try {...} catch{} bad?
The big difference is that try...catch
will swallow the exception, hiding the fact that an error occurred. try..finally
will run your cleanup code and then the exception will keep going, to be handled by something that knows what to do with it.
When to use try finally and not try catch finally
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.
Is it good practise to use finally
If you are doing any database operation in your program, then finally block
is good choice.
So,Using finally block here, It closes database automatically.
For example:
try
{
//code of operation
}
catch(Exception error)
{ //code to handle exception
error.printStackTrace();
}
finally
{ //any code which need to execute mandatory
commit();
db.close();
}
In the above example, try block
handles the opening DB connection and inserting,retrieving or updating the DB. catch block
handles exception and finally block
close all DB connection.
Why use finally instead of code after catch
Because if an exception gets thrown no code after the try
block is executed unless the exception is caught. A finally
block is always executed no matter what happens inside your try
block.
Why use Finally in Try ... Catch
Yes, it is different. Finally will always run (barring program crash). If the function exits inside of the try catch block, or another error is thrown in either the try or the catch, the finally will still execute. You won't get that functionality not using the finally statement.
Is Try-Finally to be used used sparingly for the same reasons as Try-Catch?
You don't need to avoid the try {} ... finally {} pattern of coding. But as far as your connections go, since they're IDisposable, use "using" instead, since it does the same thing for you as the longer and more cumbersome try/finally block.
Is try/catch without finally bad
Short answer, no. It all depends on what happens in your try block. I would say that most of your try-catches will probably not need finally. Finally is however required when you are opening resources in the try block such as files, streams, network etc that you have to close (whether an exception is thrown or not)
Difference between try-finally and try-catch
These are two different things:
- The catch block is only executed if an exception is thrown in the try block.
- The finally block is executed always after the try(-catch) block, if an exception is thrown or not.
In your example you haven't shown the third possible construct:
try {
// try to execute this statements...
}
catch( SpecificException e ) {
// if a specific exception was thrown, handle it here
}
// ... more catches for specific exceptions can come here
catch( Exception e ) {
// if a more general exception was thrown, handle it here
}
finally {
// here you can clean things up afterwards
}
And, like @codeca says in his comment, there is no way to access the exception inside the finally block, because the finally block is executed even if there is no exception.
Of course you could declare a variable that holds the exception outside of your block and assign a value inside the catch block. Afterwards you can access this variable inside your finally block.
Throwable throwable = null;
try {
// do some stuff
}
catch( Throwable e ) {
throwable = e;
}
finally {
if( throwable != null ) {
// handle it
}
}
What is the difference between try-finally and try-catch-throw?
Think of it like this...
try
{
//do some stuff
}
catch
{
//do some stuff if there was an exception
//maybe some cleanup, maybe rethrow exception
}
finally
{
//always do this stuff exception or not
}
Related Topics
Get List of Zero Reference Codes in Visual Studio
What Replaces Wcf in .Net Core
How to Route Images Using ASP.NET MVC Routing
Does ASP.NET MVC Have Application Variables
Namespace and Class with the Same Name
How to Include Line Numbers in a Stack Trace Without a Pdb
How to Display Items in Canvas Through Binding
Initial Capacity of Collection Types, E.G. Dictionary, List
Possible Unintended Reference Comparison
Microsoft.Office.Interop.Excel Really Slow