Answer to Question #272397 in C# for xxx

Question #272397

Exceptions:

1. Create a situation where an exception will occur

2. Create a situation where an exception will occur, and catch the exception.

3. Create a situation where an exception will occur, and catch the exception, the display a custom message.

4. Create a situation where an exception will occur, and catch the exception, then display the caught exception’s internal message.

5. Write code utilize the finally block

6. Create a situation where an exception will occur, and pass the exception to be handled up the chain.

7. Create a situation where no exception will occur, but generate an exception anyway.


1
Expert's answer
2021-11-28T18:41:46-0500
static void Main(string[] args)
    {
        //1. Create a situation where an exception will occur
        //FileNotFoindException
        File.Open("incorrect.txt", FileMode.Open);


        Console.ReadLine();
    }


    static void Main(string[] args)
    {
        //2. Create a situation where an exception will occur, and catch the exception.
        try
        {
            //FileNotFoindException
            File.Open("incorrect.txt", FileMode.Open);
        }
        catch
        {


        }
        Console.ReadLine();
    }


    static void Main(string[] args)
    {
        //3. Create a situation where an exception will occur, and catch the exception, the display a custom message.
        try
        {
            //FileNotFoindException
            File.Open("incorrect.txt", FileMode.Open);
        }
        catch
        {
            Console.WriteLine("Exception! File name or path is incorrect!");
        }


        Console.ReadLine();
    }


    static void Main(string[] args)
    {
        //4. Create a situation where an exception will occur, and catch the exception, then display the caught exception’s internal message.
        try
        {
            //FileNotFoindException
            File.Open("incorrect.txt", FileMode.Open);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }


        Console.ReadLine();
    }


    static void Main(string[] args)
    {
        //5. Write code utilize the finally block
        try
        {
            //FileNotFoindEyception
            File.Open("incorrect.txt", FileMode.Open);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            Console.WriteLine("The final block will be executed in any way!");
        }


        Console.ReadLine();
    }




    static void Main(string[] args)
    {
        //6. Create a situation where an exception will occur, and pass the exception to be handled up the chain.
        try
        {
            OpenFIle(string.Empty);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }


        Console.ReadLine();
    }




    public static void OpenFIle(string fileName)
    {
        try
        {
            File.Open(fileName, FileMode.Open);
        }
        catch (Exception)
        {
            throw;
        }
    }


    static void Main(string[] args)
    {
        //7. Create a situation where no exception will occur, but generate an exception anyway.
        try
        {
            File.Create("1.txt");
            throw new Exception("File was created exception!");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }


        Console.ReadLine();
    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS