
- 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
Built-in Exceptions in C#
Exceptions are a problem that arises when a program 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.
Example
Let us see an example to handle the 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(); } } }
Output
Result: 0
- Related Articles
- Built-in Exceptions in Java\n
- Concrete Exceptions in Python
- Custom exceptions in Java
- Chained Exceptions in C#
- Built-in javascript constructors?
- Raising an Exceptions in Python
- User-Defined Exceptions in Python
- Built-in objects in Python (builtins)
- In-built Data Structures in Python
- Built-in String Methods in Python
- Built-in Tuple Functions in Python
- Built-In Class Attributes in Python
- Explain Built-in Events in Backbone.js
- How do exceptions work in C++
- What are checked exceptions in Java?

Advertisements