- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is exception handling in C#?
Exceptions are problem that arise when a program in executed. The following keyword handles exceptions in C#.
try
A try block identifies a block of code for which particular exceptions is activated.
Catch
The catch keyword indicates the catching of an exception.
finally
Execute a given set of statements, whether an exception is thrown or not thrown.
throw
An exception is thrown when a problem shows up in a program.
Let us see an example to handle error in a C# program −
using System; namespace MyErrorHandlingApplication { class DivNumbers { int result; DivNumbers() { result = 0; } public void myDivision(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.myDivision(5, 0); Console.ReadKey(); } } }
- Related Articles
- What is Exception Handling in PHP ?
- What is exception handling in Python?
- Exception Handling Basics in C++
- Exception Handling in C++ vs Java
- C# Exception Handling Best Practices
- Exception handling and object destruction in C++
- C++ program to demonstrate exception handling
- Exception handling in PowerShell
- Comparison of Exception Handling in C++ and Java
- Handling the Divide by Zero Exception in C++
- NodeJS - Exception Handling in Asynchronous Code
- NodeJS - Exception Handling in eventful Code
- NodeJS - Exception Handling in Synchronous Code
- PHP Exception Handling with finally
- What are the best practices for exception handling in Python?

Advertisements