try except c# code example

Example 1: exception handling c#

try {
   // statements causing exception
} catch( ExceptionName e1 ) {
   // error handling code
} catch( ExceptionName e2 ) {
   // error handling code
} catch( ExceptionName eN ) {
   // error handling code
} finally {
   // statements to be executed
}

Example 2: c# try and catch

try
        {
            ProcessString(s);
        }
        catch (Exception e)
        {
            Console.WriteLine("{0} Exception caught.", e);
        }
    }
}
/*
Output:
System.ArgumentNullException: Value cannot be null.
   at TryFinallyTest.Main() Exception caught.
 * */

Example 3: c# try

// ------------ How to use Try, Catch and Finally? --------------- //
// This is often used whenever you want to prevent your program from 
// crashing due to an incorrect input given by the user 


// --->  TRY 
// Where you put the part of your code that can cause problems

// --->  CATCH
// The parameter of this will indicate which "exception" was the 
// root of the problem. If you don't know the cause, then you can 
// make this general, with just: catch (Exception).

// --->  FINALLY
// This block of code will always run, regardless of whether 
// "try" or "catch" were trigger or not


using System;

namespace teste2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a Number:");

            string input = Console.ReadLine();

            try
            {
                int inputNumber = int.Parse(input);  // if there is an error during the conversion, then we jump to a "catch" 
                Console.WriteLine("The power of value {0} is {1}", inputNumber, inputNumber * inputNumber);  
            }
            catch (FormatException)
            {
                Console.WriteLine("Format Exception detected: The input needs to be a number");
            }
            catch (OverflowException)
            {
                Console.WriteLine("Overflow Exception detected: The input can't be that long");
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("Argument Null Exception detected: The input can't be null");
            }
            finally
            {
                Console.ReadKey();
            }
    
        }
    }
}

Example 4: c# try

int n;
    try
    {
        // Do not initialize this variable here.
        n = 123;
    }
    catch
    {
    }
    // Error: Use of unassigned local variable 'n'.
    Console.Write(n);

Example 5: c# except

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        // Contains four values.
        int[] values1 = { 1, 2, 3, 4 };

        // Contains three values (1 and 2 also found in values1).
        int[] values2 = { 1, 2, 5 };

        // Remove all values2 from values1.
        var result = values1.Except(values2);

        // Show.
        foreach (var element in result)
        {
            Console.WriteLine(element);
        }
    }
}
/*
result
3
4
*/