I thought I had understood how exception handling in C# works. Re-reading the documentation for fun and self-confidence, I have run into problems:
This document claims that the following two code snippets are equivalent, even more, that the first one is translated to the latter one at compile time.
using (Font font1 = new Font("Arial", 10.0f)) {
byte charset = font1.GdiCharSet;
}
and
{
Font font1 = new Font("Arial", 10.0f);
try {
byte charset = font1.GdiCharSet;
}
finally {
if (font1 != null)
((IDisposable)font1).Dispose();
}
}
Furthermore, it claims:
The using statement ensures that Dispose is called even if an
exception occurs while you are calling methods on the object.
In contrast, that document states:
Within a handled exception, the associated finally block is guaranteed
to be run. However, if the exception is unhandled, execution of the
finally block is dependent on how the exception unwind operation is
triggered.
I do not get this together. In the code example from the first document, the exception clearly is unhandled (since there is no catch block). Now, if the statement from the second document is true, the finally block is not guaranteed to execute. This ultimately contradicts what the first document says ("The using statement ensures ...") (emphasis mine).
So what is the truth?
EDIT 1
I still don't get it. StevieB's answer has made me read more parts from the C# language specification. In section 16.3, we have:
[...] This search continues until a catch clause is found that can
handle the current exception [...] Once a matching catch clause is
found, the system prepares to transfer control to the first statement
of the catch clause. Before execution of the catch clause begins, the
system first executes, in order, any finally clauses that were
associated with try statements more nested that than the one that
caught the exception.
So have I made a simple test program which contains code which produces a division by zero and is within a try block. That exception is never caught in any of my code, but the respective try statement has a finally block:
int b = 0;
try {
int a = 10 / b;
}
finally {
MessageBox.Show("Hello");
}
Initially, according to the documentation snippet above, I had expected that the finally block never would be executed and that the program just would die when being executed without a debugger attached. But this is not the case; instead, the "exception dialog box" we all know too well is shown, and after that, the "Hello" dialog box appears.
After thinking a while about that and after having read docs, articles and questions like this and that, it became clear that this "exception dialog box" is produced by a standard exception handler which is built into Application.Run() and the other usual methods which could "start" your program, so I am not wondering any more why the finally block is run.
But I am still totally baffled because the "Hello" dialog appears after the "exception dialog box". The documentation snippet above is pretty clear (well, probably I am just too silly again):
The CLR won't find a catch clause which is associated with the try statement where the division by zero happens. So it should pass the exception up one level to the caller, won't find a matching catch clause there as well (there is not even a try statement there) and so on (as noted above, I do not handle (i.e. catch) any exception in this test program).
Finally, the exception should meet the CLR's default catch-all exception handler (i.e. that one which is by default active in Application.Run() and its friends), but (according to the documentation above) the CLR should now execute all finally blocks which are more deeply nested than that default handler ("my" finally block belongs to these, doesn't it?) before executing the CLR catch-all default handler's catch block.
That means that the "Hello" dialog should appear before the "exception dialog box", doesn't it?. Well, obviously, it's the other way around. Could somebody elaborate on that?
This document claims that the following two code snippets are equivalent
They are.
The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object.
Pretty much.
This ultimately contradicts what the first document says
Well, the first one was being a bit too vague, rather than flat-out incorrect.
There are cases where a finally will not run, including that implied by a using. A StackOverflowException would be one example (a real one from overflowing the stack, if you just do throw new StackOverflowException() the finally will run).
All the examples are things you can't catch, and your application is going down, so if the clean up from using is only important while the application is running, then finally is fine.
If the clean-up is vital even when the program crashes, then finally can never be enough, as it can't deal with e.g. a power plug being pulled out, which in the sort of cases where clean up is vital even in a crash, is a case that needs to be considered.
In any case where the exception is caught further up and the program continues, the finally will run.
With catchable exceptions that aren't caught, then finally blocks will generally run, but there are still some exceptions. One would be if the try-finally was inside a finaliser and the try took a long time; after a while on the finaliser queue the application will just fail-fast.
If your definition of "unhandled exception" (that it must be handled by a catch clause within the same try block) was correct, there would be no reason to ever allow the construct
try {
...
}
finally {
...
}
Since by your definition, the finally block would never run. Since the above construct is valid, we must conclude that your definition of "unhandled exception" is incorrect.
What it means is "if the exception is not handled by any exception handler, anywhere in the call stack".
You have to give the documentation a little leeway. In most circumstances there's an implied "within reason". For example, if I turn my computer off none of those finally blocks or using/Disposes will be called.
Apart from the computer shutting off, there are a handful of circumstances that OS can terminate your application--effectively shutting it off. In these circumstances Dispose and finally won't be invoked. Those are usually pretty serious error conditions like out of stack or out of memory. There are some complex scenarios where that can happen with less-than-exceptional-exceptions. For example, if you have native code that creates a managed object on a background thread and that managed object and something on that thread throws an native exception, it's likely that the managed exception handlers won't get called (e.g. Dispose) because the OS will just terminate the thread and anything that could have been disposed is not accessible any more.
But, yes, those statements are effectively equivalent and within reason the finally block will be executed and Dispose will be called.
The C# language specification states that the finally blocks will be executed (be that in a using statement or elsewhere) for System.Exception or any of its derived exceptions. Of course, if you get an exception that can't be handled with the usual try..catch logic e.g. AccessViolationException all bets are off which is where the ambiguity comes in.
The spec is installed with Visual Studio 2013 and later - with 2017 it's in C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC#\Specifications\1033
In section
8.9.5 The throw statement
we see the following:
When an exception is thrown, control is transferred
to the first catch clause in an enclosing try statement that can
handle the exception. The process that takes place from the point of
the exception being thrown to the point of transferring control to a
suitable exception handler is known as exception propagation.
Propagation of an exception consists of repeatedly evaluating the
following steps until a catch clause that matches the exception is
found. In this description, the throw point is initially the location
at which the exception is thrown.
In the current function member,
each try statement that encloses the throw point is examined. For each
statement S, starting with the innermost try statement and ending with
the outermost try statement, the following steps are evaluated:
If
the try block of S encloses the throw point and if S has one or more
catch clauses, the catch clauses are examined in order of appearance
to locate a suitable handler for the exception. The first catch clause
that specifies the exception type or a base type of the exception type
is considered a match. A general catch clause (§8.10) is considered a
match for any exception type. If a matching catch clause is located,
the exception propagation is completed by transferring control to the
block of that catch clause.
Otherwise, if the try block or a catch
block of S encloses the throw point and if S has a finally block,
control is transferred to the finally block. If the finally block
throws another exception, processing of the current exception is
terminated. Otherwise, when control reaches the end point of the
finally block, processing of the current exception is continued.
If
an exception handler was not located in the current function
invocation, the function invocation is terminated, and one of the
following occurs:
If the current function is non-async, the steps
above are repeated for the caller of the function with a throw point
corresponding to the statement from which the function member was
invoked.
If the current function is async and task-returning, the
exception is recorded in the return task, which is put into a faulted
or cancelled state as described in §10.14.1.
If the current function
is async and void-returning, the synchronization context of the
current thread is notified as described in §10.14.2.
If the
exception processing terminates all function member invocations in the
current thread, indicating that the thread has no handler for the
exception, then the thread is itself terminated. The impact of such
termination is implementation-defined.
I believe (correct me if I am wrong) the answer is lying in the definition:
The using statement ensures that Dispose is called even if an
exception occurs while you are calling methods on the object.
So if any exceptions occur in any methods called by the object of using, finally is ensured to run. On the other hand finally is not guaranteed to run if inside the using block call other methods not related with the object cause an exception
First of all, I would like to bring to your notice that using statement cannot be used for all types. This can be used only for types that implement IDisposable interface which has a functionality to dispose the object automatically. This is present in the second document you mentioned
C# also contains the using statement, which provides similar functionality for IDisposable objects in a convenient syntax.
This means if an unhandled exception happens, cleaning up of your objects is handled by the Dispose() method of the type(this is given for using statement documentation)
Coming to the query, even though your finally block(generated) is not guarenteed to run for unhandled exceptions, the object dispose operation is handled during runtime by .Net CLR
Hope this clears your doubt
Given the following code in C#:
public void CatchExceptionThenThrow()
{
try
{
StartThings();
}
catch (Exception)
{
throw;
}
}
I have converted that to VB as such using the dotnetfiddle VB.net converter:
Public Sub CatchExceptionThenThrow()
Try
StartThings()
Catch As Exception
Throw
End Try
End Sub
This throws a compile error on:
Catch As Exception
End of Statement expected
I then change that to:
Public Sub CatchExceptionThenThrow()
Try
StartThings()
Catch ex As Exception
Throw
End Try
End Sub
But this creates a warning "variable declared but never used". How do I go about throwing rather than throw exing in VB without getting the warning, all the while preserving the entirety of the stack trace as in the first C# example?
All good comments, and thanks for the redundancy information I realize the try/catch is completely not needed as this would have occurred with or without the try/catch. The question was more for curiosities sake in a scenario that, I suppose, has no real basis in (a good code) reality.
I had seen something similar in a blog post about exception handling recently and why to throw vs throw ex, and was just curious as to how to accomplish the same code in VB - as I'm not strong with VB and am trying to better understand it, and exception handling.
I had hoped I'd be able to find the blog post I referenced above, but was unable. The gist of it (which spawned the question) can be found: https://dotnetfiddle.net/741wAi
Just have an empty Catch like:
Try
StartThings()
Catch
Throw
End Try
But if you are not doing anything in the Catch block other than re-throwing it, then there is no point to have try-catch in first place.
You can have StartThings() without try-catch and in case of exception, the exception will propagate to the caller.
The reason you are getting warning for Catch ex As Exception, is that you caught the exception in variable ex but you are not using it anywhere.
If you're just catching Exception then your code is redundant, as has been pointed out. If however you're simplifying your example code and you are trying to catch only a specific type of exception and do other processing for other thrown Exceptions, then I hate to break it to you but it appears VB can't really do this so you'll have to put up with the warning. Obviously don't throw ex.
I want the code calling a function to handle any exception raised in the function. If I write:
try
{
// Code than may raise an exception
}
catch
{
throw;
}
The exception will be passed back with the callstack. Could I write the following instead and get the same result? Is there any reason to use the try catch in this case?
// Code that may raise an exception
In the scenario you've presented, the only reason to catch, and then rethrow, the exception, is if you're doing something else in the catch block, like logging or cleanup. Otherwise, it's entirely a no-op.
Good on you that you're using throw, rather than throw e, by the way, if you do need this construct. The former preserves the callstack; the latter does not.
There is no reason to use try/catch in that case.
If you were logging any information or encapsulating the exception in a higher-level one, then the try/catch would be indicated.
Can anyone please explain me use of throw in exception handling?
What happens when i throw an exception?
It means to "cause" an exception. When you "throw" an exception you are saying "something has gone wrong, here's some details".
You can then "catch" a "thrown" exception to allow your program to degrade gracefully instead of erroring and dying.
"Throwing" an exception is what triggers the entire process of exception handling.
In the course of normal execution, lines in a program are executed sequentially with loops and branches. When an error of some sort happens, an exception is created and then thrown.
A thrown exception will modify the usual order of operations in a program in such a way that no "normal" instructions will be executed until the exception is handled within a "catch" block somewhere. Once an exception is caught in a catch block, and the code within that catch block is executed ("Handling" the exception), normal program execution will resume immediately following the catch block.
// Do some stuff, an exception thrown here won't be caught.
try
{
// Do stuff
throw new InvalidOperationException("Some state was invalid.");
// Nothing here will be executed because the exception has been thrown
}
catch(InvalidOperationException ex) // Catch and handle the exception
{
// This code is responsible for dealing with the error condition
// that prompted the exception to be thrown. We choose to name
// the exception "ex" in this block.
}
// This code will continue to execute as usual because the exception
// has been handled.
When you throw an exception you're basically saying that some condition has happened beyond the reasonable means of the caller being expected to handle it. They're especially useful in constructors which have no way of signaling any form of construction failure (as they don't have return values).
When you throw an exception the runtime moves up the execution chain until it finds a catch block that is assignable to the type of exception you've thrown. On the way it runs the code in any finally blocks you may have, which allows you to (typically) release any resources you may have acquired.
The throw creates the exception to be handled. The object you passed then becomes the data that describes the exception.
Until something is thrown there is no exception to be handled.
Throwing an exception causes the exception to rise up the stack. There are two primary scenarios for a throw.
Have an exceptional condition unique to your code
if(inputVal < 0)
{
throw new LessThanZeroCustomException("You cannot enter a value less than zero");
}
The above code assumes you have coded an exception object called LessThanZeroCustomException. I would not actually name it this, but the Custom in the name is designed to illustrate you coded this. It would most likely inherit from
Have an exceptional condition that has been caught and needs to be rethrown. The normal reason for this is logging. In most cases, I dislike this pattern, as you end up spending time catching, logging and throwing over and over again. This is because most people doing this pattern try ... catch at every level. Yuck!
In short, throw means "I found an exceptional condition I cannot handle, so I am letting the person using this code know by throwing an exception".
I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything:
StreamReader reader=new StreamReader("myfile.txt");
try
{
int i = 5 / 0;
}
catch // No args, so it will catch any exception
{}
reader.Close();
However, this is considered good form:
StreamReader reader=new StreamReader("myfile.txt");
try
{
int i = 5 / 0;
}
finally // Will execute despite any exception
{
reader.Close();
}
As far as I can tell, the only difference between putting cleanup code in a finally block and putting cleanup code after the try..catch blocks is if you have return statements in your try block (in that case, the cleanup code in finally will run, but code after the try..catch will not).
Otherwise, what's so special about finally?
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.
"Finally" is a statement of "Something you must always do to make sure program state is sane". As such, it's always good form to have one, if there's any possibility that exceptions may throw off the program state. The compiler also goes to great lengths to ensure that your Finally code is run.
"Catch" is a statement of "I can recover from this exception". You should only recover from exceptions you really can correct - catch without arguments says "Hey, I can recover from anything!", which is nearly always untrue.
If it were possible to recover from every exception, then it would really be a semantic quibble, about what you're declaring your intent to be. However, it's not, and almost certainly frames above yours will be better equipped to handle certain exceptions. As such, use finally, get your cleanup code run for free, but still let more knowledgeable handlers deal with the issue.
Because when that one single line throws an exception, you wouldn't know it.
With the first block of code, the exception will simply be absorbed, the program will continue to execute even when the state of the program might be wrong.
With the second block, the exception will be thrown and bubbles up but the reader.Close() is still guaranteed to run.
If an exception is not expected, then don't put a try..catch block just so, it'll be hard to debug later when the program went into a bad state and you don't have an idea why.
Finally is executed no matter what. So, if your try block was successful it will execute, if your try block fails, it will then execute the catch block, and then the finally block.
Also, it's better to try to use the following construct:
using (StreamReader reader=new StreamReader("myfile.txt"))
{
}
As the using statement is automatically wrapped in a try / finally and the stream will be automatically closed. (You will need to put a try / catch around the using statement if you want to actually catch the exception).
While the following 2 code blocks are equivalent, they are not equal.
try
{
int i = 1/0;
}
catch
{
reader.Close();
throw;
}
try
{
int i = 1/0;
}
finally
{
reader.Close();
}
'finally' is intention-revealing code. You declare to the compiler and to other programmers that this code needs to run no matter what.
if you have multiple catch blocks and you have cleanup code, you need finally. Without finally, you would be duplicating your cleanup code in each catch block. (DRY principle)
finally blocks are special. The CLR recognizes and treats code withing a finally block separately from catch blocks, and the CLR goes to great lengths to guarantee that a finally block will always execute. It's not just syntactic sugar from the compiler.
I agree with what seems to be the consensus here - an empty 'catch' is bad because it masks whatever exception might have occurred in the try block.
Also, from a readability standpoint, when I see a 'try' block I assume there will be a corresponding 'catch' statement. If you are only using a 'try' in order to ensure resources are de-allocated in the 'finally' block, you might consider the 'using' statement instead:
using (StreamReader reader = new StreamReader('myfile.txt'))
{
// do stuff here
} // reader.dispose() is called automatically
You can use the 'using' statement with any object that implements IDisposable. The object's dispose() method gets called automatically at the end of the block.
Use Try..Catch..Finally, if your method knows how to handle the exception locally. The Exception occurs in Try, Handled in Catch and after that clean up is done in Finally.
In case if your method doesn't know how to handle the exception but needs a cleanup once it has occurred use Try..Finally
By this the exception is propagated to the calling methods and handled if there are any suitable Catch statements in the calling methods.If there are no exception handlers in the current method or any of the calling methods then the application crashes.
By Try..Finally it is ensured that the local clean up is done before propagating the exception to the calling methods.
The try..finally block will still throw any exceptions that are raised. All finally does is ensure that the cleanup code is run before the exception is thrown.
The try..catch with an empty catch will completely consume any exception and hide the fact that it happened. The reader will be closed, but there's no telling if the correct thing happened. What if your intent was to write i to the file? In this case, you won't make it to that part of the code and myfile.txt will be empty. Do all of the downstream methods handle this properly? When you see the empty file, will you be able to correctly guess that it's empty because an exception was thrown? Better to throw the exception and let it be known that you're doing something wrong.
Another reason is the try..catch done like this is completely incorrect. What you are saying by doing this is, "No matter what happens, I can handle it." What about StackOverflowException, can you clean up after that? What about OutOfMemoryException? In general, you should only handle exceptions that you expect and know how to handle.
If you don't know what exception type to catch or what to do with it, there's no point in having a catch statement. You should just leave it for a higher-up caller that may have more information about the situation to know what to do.
You should still have a finally statement in there in case there is an exception, so that you can clean up resources before that exception is thrown to the caller.
From a readability perspective, it's more explicitly telling future code-readers "this stuff in here is important, it needs to be done no matter what happens." This is good.
Also, empty catch statements tend to have a certain "smell" to them. They might be a sign that developers aren't thinking through the various exceptions that can occur and how to handle them.
Finally is optional -- there's no reason to have a "Finally" block if there are no resources to clean up.
Taken from: here
Raising and catching exceptions should not routinely occur as part of the successful execution of a method. When developing class libraries, client code must be given the opportunity to test for an error condition before undertaking an operation that can result in an exception being raised. For example, System.IO.FileStream provides a CanRead property that can be checked prior to calling the Read method, preventing a potential exception being raised, as illustrated in the following code snippet:
Dim str As Stream = GetStream()
If (str.CanRead) Then
'code to read stream
End If
The decision of whether to check the state of an object prior to invoking a particular method that may raise an exception depends on the expected state of the object. If a FileStream object is created using a file path that should exist and a constructor that should return a file in read mode, checking the CanRead property is not necessary; the inability to read the FileStream would be a violation of the expected behavior of the method calls made, and an exception should be raised. In contrast, if a method is documented as returning a FileStream reference that may or may not be readable, checking the CanRead property before attempting to read data is advisable.
To illustrate the performance impact that using a "run until exception" coding technique can cause, the performance of a cast, which throws an InvalidCastException if the cast fails, is compared to the C# as operator, which returns nulls if a cast fails. The performance of the two techniques is identical for the case where the cast is valid (see Test 8.05), but for the case where the cast is invalid, and using a cast causes an exception, using a cast is 600 times slower than using the as operator (see Test 8.06). The high-performance impact of the exception-throwing technique includes the cost of allocating, throwing, and catching the exception and the cost of subsequent garbage collection of the exception object, which means the instantaneous impact of throwing an exception is not this high. As more exceptions are thrown, frequent garbage collection becomes an issue, so the overall impact of the frequent use of an exception- throwing coding technique will be similar to Test 8.05.
It's bad practice to add a catch clause just to rethrow the exception.
If you'll read C# for programmers you will understand, that the finally block was design to optimize an application and prevent memory leak.
The CLR does not completely eliminate leaks... memory leaks can occur if program inadvertently keep references to unwanted objects
For example when you open a file or database connection, your machine will allocate memory to cater that transaction, and that memory will be kept not unless the disposed or close command was executed. but if during transaction, an error was occurred, the proceeding command will be terminated not unless it was inside the try.. finally.. block.
catch was different from finally in the sense that, catch was design to give you way to handle/manage or interpret the error it self. Think of it as person who tells you "hey i caught some bad guys, what do you want me to do to them?"
while finally was designed to make sure that your resources was properly placed. Think of it of someone that whether or not there is some bad guys he will make sure that your property was still safe.
And you should allow those two to work together for good.
for example:
try
{
StreamReader reader=new StreamReader("myfile.txt");
//do other stuff
}
catch(Exception ex){
// Create log, or show notification
generic.Createlog("Error", ex.message);
}
finally // Will execute despite any exception
{
reader.Close();
}
With finally, you can clean up resources, even if your catch statement throws the exception up to the calling program. With your example containing the empty catch statement, there is little difference. However, if in your catch, you do some processing and throw the error, or even just don't even have a catch at all, the finally will still get run.
Well for one, it's bad practice to catch exceptions you don't bother to handle. Check out Chapter 5 about .Net Performance from Improving .NET Application Performance and Scalability. Side note, you should probably be loading the stream inside the try block, that way, you can catch the pertinent exception if it fails. Creating the stream outside the try block defeats its purpose.
Amongst probably many reasons, exceptions are very slow to execute. You can easily cripple your execution times if this happens a lot.
The problem with try/catch blocks that catch all exceptions is that your program is now in an indeterminate state if an unknown exception occurs. This goes completely against the fail fast rule - you don't want your program to continue if an exception occurs. The above try/catch would even catch OutOfMemoryExceptions, but that is definitely a state that your program will not run in.
Try/finally blocks allow you to execute clean up code while still failing fast. For most circumstances, you only want to catch all exceptions at the global level, so that you can log them, and then exit out.
The effective difference between your examples is negligible as long as no exceptions are thrown.
If, however, an exception is thrown while in the 'try' clause, the first example will swallow it completely. The second example will raise the exception to the next step up the call stack, so the difference in the stated examples is that one completely obscures any exceptions (first example), and the other (second example) retains exception information for potential later handling while still executing the content in the 'finally' clause.
If, for example, you were to put code in the 'catch' clause of the first example that threw an exception (either the one that was initially raised, or a new one), the reader cleanup code would never execute. Finally executes regardless of what happens in the 'catch' clause.
So, the main difference between 'catch' and 'finally' is that the contents of the 'finally' block (with a few rare exceptions) can be considered guaranteed to execute, even in the face of an unexpected exception, while any code following a 'catch' clause (but outside a 'finally' clause) would not carry such a guaranty.
Incidentally, Stream and StreamReader both implement IDisposable, and can be wrapped in a 'using' block. 'Using' blocks are the semantic equivalent of try/finally (no 'catch'), so your example could be more tersely expressed as:
using (StreamReader reader = new StreamReader("myfile.txt"))
{
int i = 5 / 0;
}
...which will close and dispose of the StreamReader instance when it goes out of scope.
Hope this helps.
try {…} catch{} is not always bad. It's not a common pattern, but I do tend to use it when I need to shutdown resources no matter what, like closing a (possibly) open sockets at the end of a thread.