Hi All, 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.
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 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.
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 si 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". Hope this helps!