
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Flow control in try catch finally in C#
The flow control in try, catch, and finally can be understood using the following example. Here, we are dividing two numbers −
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
Exception caught: System.DivideByZeroException: Attempted to divide by zero. at ErrorHandlingApplication.DivNumbers.division (System.Int32 num1, System.Int32 num2) [0x00000] in :0 Result: 0
The following shows the flow control in exceptions handling using try catch finally in C#
- If an exception occurs in try block, then the control transfers to the catch block.
- After the catch block finish, then the finally block works.
- If no exception occurs, then firstly the try occurs and then the flow control to finally block
- Related Questions & Answers
- Flow control in try catch finally in Java
- Flow control in a try catch finally in Java
- Flow control in try catch finally in Java programming.
- Try-Catch-Finally in C#
- Try/catch/finally/throw keywords in C#
- Explain Try/Catch/Finally block in PowerShell
- What are try, catch, finally blocks 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
- How to use Try/catch blocks in C#?
- Can we declare a try catch block within another try catch block in Java?
- Static Control Flow in Java
- Control flow alterations in Ruby
Advertisements