Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using data;
- namespace CsharpCourse
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- // you must use catch or finally with try
- // finally -> always executes
- try
- {
- int x = int.Parse("hello"); // if the exception occurs , it breaks from the try block
- Console.WriteLine(Divide(9, 3));
- Console.WriteLine(Divide(10, 0));
- Console.WriteLine(Divide(10, 2));
- }
- catch(DivideByZeroException ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine( "Can not divide By zero" ,ex.ToString());
- Console.ForegroundColor = ConsoleColor.White;
- }
- catch (Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine( ex.ToString() );
- Console.ForegroundColor = ConsoleColor.White;
- }
- finally
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Finally I am here!");
- Console.ForegroundColor = ConsoleColor.White;
- }
- // if you want to make your exception -> throw
- try
- {
- Console.WriteLine("Enter a number with five digits");
- string input = Console.ReadLine();
- if(input.Length != 5)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- throw new Exception("IT is not a Five-digit number!");
- Console.ForegroundColor = ConsoleColor.White;
- }
- }
- finally
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Try Again Later");
- Console.ForegroundColor = ConsoleColor.White;
- }
- }
- static int Divide(int value , int divisor) {
- return value/divisor;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement