
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Try-Catch-Finally in C#
C# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.
C# exception handling is performed using the following keywords −
try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.
catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
finally − The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.
The following is an example showing how to handle exceptions in C# −
Example
using System; namespace ErrorHandlingApplication { class DivNumbers { int result; DivNumbers() { result = 0; } public void division(int num1, int num2) { try { result = num1 / num2; } catch (DivideByZeroException e) { Console.WriteLine("Exception caught: {0}", e); } finally { Console.WriteLine("Result: {0}", result); } } static void Main(string[] args) { DivNumbers d = new DivNumbers(); d.division(25, 0); Console.ReadKey(); } } }
Output
Above, we have set the values in a try, and then caught exceptions in the catch. Finally is also set to show the result −
try { result = num1 / num2; } catch (DivideByZeroException e) { Console.WriteLine("Exception caught: {0}", e); } finally { Console.WriteLine("Result: {0}", result); }
- Related Articles
- Try/catch/finally/throw keywords in C#
- Explain Try/Catch/Finally block in PowerShell
- Flow control in try catch finally in C#
- What are try, catch, finally blocks in Java?
- Flow control in try catch finally in Java programming.
- Flow control in a try catch finally in Java
- How do we use try...catch...finally statement in JavaScript?
- Can we write any statements between try, catch and finally blocks in Java?
- Why variables defined in try cannot be used in catch or finally in java?
- The try-finally Clause in Python
- Can we declare a try catch block within another try catch block in Java?
- Explain try, except and finally statements in Python.
- Try, catch, throw and throws in Java
- How to use Try/catch blocks in C#?
- Can finally block be used without catch in Java?
