C# Exception Handling Interview Questions
Exception handling in C# is an important part of .NET, especially when dealing with unknown input / output such as files, database or network connections.
Good exception handling can let you recover from the kind of programming error that is likely to crash your program. Think of it like sticking a piece of twine onto a method. If the method fails, you can rewind the twine to where you started and notify the user that there is a problem or attempt recovery.
Exception handling works with three keywords:
TRY - Which is the block where you write the code you want to handle.
CATCH - which is the block where you recover and can examine the exception to determine a course of action.
and FINALLY which is guaranteed to be executed if TRY or CATCH is run so that you can clean up connections.
Exceptions should be reserved for truly EXCEPTIONAL situations, not just used for regular program control.
A TRY statement can support multiple CATCH statements, each of which may catch a different kind of exception. TRY doesn't always need to be paired with a CATCH. You can also use TRY-FINALLY which is the equivalent of the USING statement. The code for this can be found at: ryanmcbeth.com
Kommentare