TheDeveloperBlog.com

Home | Contact Us

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

C# Try Keyword

This C# article introduces the try keyword. Try describes protected regions of code.

Try. The try keyword begins an exception handling block.

Control flow enters a protected region. We already know that try is used to implement exception handling, but it is useful to take a closer look.

Example. In this program, two methods are present. We see the A method, which uses the try and catch keywords, and the B method, which does not. In A, the try denotes that a protected region of code begins.

Note: This means when the DivideByZeroException is thrown, the catch block will be entered.

DivideByZeroException

C# program that shows try keyword

using System;

class Program
{
    static void Main()
    {
	A();
	B();
    }

    static void A()
    {
	try
	{
	    int value = 1 / int.Parse("0");
	}
	catch
	{
	    Console.WriteLine("A");
	}
    }

    static void B()
    {
	int value = 1 / int.Parse("0");
	Console.WriteLine("B");
    }
}

Output

A

Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
   at Program.B() in C:\...\Program.cs:line 25
   at Program.Main() in C:\...\Program.cs:line 8

Protected regions. To see what exception handing actually does, we next look at the intermediate representation (IL). When a method uses exception handling, the IL shows an ending descriptor (.try, to, catch object handler, to).

Intermediate LanguageCatch

Note: This tells the virtual execution engine how to execute the statements in the method in those ranges.

Intermediate representation for A: IL

.method private hidebysig static void A() cil managed
{
    .maxstack 2
    L_0000: ldc.i4.1
    L_0001: ldstr "0"
    L_0006: call int32 [mscorlib]System.Int32::Parse(string)
    L_000b: div
    L_000c: pop
    L_000d: leave.s L_001c
    L_000f: pop
    L_0010: ldstr "A"
    L_0015: call void [mscorlib]System.Console::WriteLine(string)
    L_001a: leave.s L_001c
    L_001c: ret
    .try L_0000 to L_000f catch object handler L_000f to L_001c
}

There is no "try" opcode instruction in the same way there is a call instruction. Exception handling is built into the execution engine at all levels. The engine knows at every statement whether it is inside a protected region.

Thus: Try is a keyword that modifies many statements, not an imperative opcode.

Finally. The try keyword is not only used to deal with potential errors in code. It is also used with the finally keyword. A finally block executes always after the completion of the try block statements.

Finally

In this program, no errors occur. It is unlikely that Console.WriteLine will throw an exception here. And the try-finally blocks are likely unnecessary. The program shows the try statement's use in the absence of exceptions.

Console.WriteLine

Problem that uses try with finally: C#

using System;

class Program
{
    static void Main()
    {
	try
	{
	    Console.WriteLine("A");
	}
	finally
	{
	    Console.WriteLine("B");
	}
    }
}

Output

A
B

We used the try keyword. And we tried to understand its implementation. It specifies a range of protected statements. This functionality is built into the virtual execution engine at a deep level.

Review: Try is an important and useful keyword in the C# language. It helps with the development of error-proof, reliable code.


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