How to use catch block in C#

3 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                DivideByZero();
            }
            catch
            {
                Console.WriteLine("catch");
            }
        }
        static int DivideByZero()
        {
            return 1 / int.Parse("0");
        }
    }
}


/*
run:

catch

*/

 



answered Mar 7, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                DivideByZero();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static int DivideByZero()
        {
            return 1 / int.Parse("0");
        }
    }
}


/*
run:

Attempted to divide by zero.

*/

 



answered Mar 7, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                DivideByZero();
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("catch 1");
            }
            catch
            {
                Console.WriteLine("catch 2");
            }
        }
        static int DivideByZero()
        {
            return 1 / int.Parse("0");
        }
    }
}


/*
run:

catch 1

*/

 



answered Mar 7, 2017 by avibootz

Related questions

4 answers 515 views
2 answers 544 views
1 answer 301 views
1 answer 323 views
2 answers 496 views
1 answer 252 views
1 answer 338 views
338 views asked Nov 23, 2020 by avibootz
...