C# try/catch - c# - c# tutorial - c# net




What is the use of try/catch statement in C# ?

  • In C# programming, exception handling is performed by try/catch statement.
  • The try block in C# is used to place the code that may throw exception.
  • The catch block is used to handled the exception.
  • The catch block must be preceded by try block.
 Try Catch Exception

Try Catch Exception

C# example without try/catch

using System;  

public class ExExample  

{  

public static void Main(string[] args)  

{  

int a = 10;  

int b = 0;  

int x = a/b;    

Console.WriteLine("Rest of the code");  

}  

}  
click below button to copy the code. By - c# tutorial - team
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

C# examples - Output :

Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.

C# try/catch example

 Try Catch Exception

Try Catch Exception

using System;  

public class ExExample  

{  

public static void Main(string[] args)  

{  

try  

{  

int a = 10;  

int b = 0;  

int x = a / b;  

}  

catch (Exception e) { Console.WriteLine(e); }  

Console.WriteLine("Rest of the code");  

}  

}  
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

System.DivideByZeroException: Attempted to divide by zero.
Rest of the code



Related Searches to C# try/catch

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×