0

Say you are running a program,

and it meets a "THROW" statement... what happens? Will the program stop? Will it continue?

And what's "FINALLY" for?

Please I appreciate an explanation in simple words

user1034912
  • 2,153
  • 7
  • 38
  • 60

4 Answers4

1

if program meeets throw instruction it will throw an exception.

Will your application stop or continue running depends on will you handle that exception ot not with catch instruction.

finally, instead, is introduced to guarantee the execution of the containing code inside that block either exception was thrown or not.

Tigran
  • 61,654
  • 8
  • 86
  • 123
1

See the MSDN documentation for throw here: http://msdn.microsoft.com/en-us/library/1ah5wsex.aspx

In brief, throw raises an exception. If you are in a try-catch block then it will be caught, if not your program may crash.

The finally block executes after the try-catch block regardless of whether there was an exception which was thrown (and caught).

Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
1

The throw statement is used to signal the occurrence of an anomalous situation (exception) during the program execution. Usually the throw statement is used with try-catch or try-finally statements. When an exception is thrown, the program looks for the catch statement that handles this exception.

The finally block is useful for cleaning up any resources allocated in the try block as well as running any code that must execute even if there is an exception. Control is always passed to the finally block regardless of how the try block exits.

Throw: http://msdn.microsoft.com/en-us/library/1ah5wsex(v=vs.80).aspx

Finally: http://msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.80).aspx

benni_mac_b
  • 8,803
  • 5
  • 39
  • 59
1

You find a lot of information here:

http://msdn.microsoft.com/en-us/library/ms173160(v=vs.80).aspx

Exceptions have the following properties:

  • When your application encounters an exceptional circumstance, such as a division by zero or low memory warning, an exception is generated.
  • Use a try block around the statements that might throw exceptions.
  • Once an exception occurs within the try block, the flow of control immediately jumps to an associated exception handler, if one is present.
  • If no exception handler for a given exception is present, the program stops executing with an error message.
  • If a catch block defines an exception variable, you can use it to get more information on the type of exception that occurred.
  • Actions that may result in an exception are executed with the try keyword.
  • An exception handler is a block of code that is executed when an exception occurs. In C#, the catch keyword is used to define an exception handler.
  • Exceptions can be explicitly generated by a program using the throw keyword.
  • Exception objects contain detailed information about the error, including the state of the call stack and a text description of the error.
  • Code in a finally block is executed even if an exception is thrown, thus allowing a program to release resources.
webber2k6
  • 648
  • 9
  • 17