TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Try Catch Reference and Examples

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. Try-Catch statements are commonly used in C# programs and it is the best practice handle any runtime errors.

Try-Catch. In C# try-catch we handle exceptions. An alternative flow, the try-catch pattern traps errors, separating them in a clear way. Programs become easier to read. The catch block allows an optional variable.

Try-catch statement in a C# program is a the best practice and you should start using it if you are not sure about any run time errors. 

Try

Tip: Multiple catch blocks can be stacked to provide more control. The final one does not require a variable type.

Example. This program shows three patterns of using try-catch blocks. Please notice the styraxes used in the catch blocks. After the catch keyword, we use parentheses to declare an exception variable. This variable can optionally be named.

Note: In the third example, we have more than one catch block in a row. The most general catch comes last.

Based on:

.NET 4.5

C# program that uses catch blocks

using System;

class Program
{
    static void Main()
    {
	// You can use an empty catch block.
	try
	{
	    DivideByZero();
	}
	catch
	{
	    Console.WriteLine("0");
	}
	// You can specify a variable in the catch.
	try
	{
	    DivideByZero();
	}
	catch (Exception ex)
	{
	    Console.WriteLine("1");
	}
	// You can use multiple catch blocks.
	try
	{
	    DivideByZero();
	}
	catch (DivideByZeroException)
	{
	    Console.WriteLine("2");
	}
	catch
	{
	    Console.WriteLine("3");
	}
    }

    static int DivideByZero()
    {
	int value1 = 1;
	int value2 = int.Parse("0");
	return value1 / value2;
    }
}

Output

0
1
2

The program throws an exception three times. Each call to the DivideByZero method causes an exception to be raised. It does this by dividing by zero. We use int.Parse to prevent the compiler from spotting the error.

DivideByZeroExceptionParseCompile-Time Error

Then: Three lines are written to screen. These numbers indicate what catch blocks were executed in the control flow.

Console.WriteLine

Summary. In complex programs, with many errors, exception handling is necessary. The catch block is a key part of exception handling. By going outside of the imperative, C-based control flow, we more easily trap unexpected conditions.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf