Avi
Avi

Reputation: 2283

From C# code to DAG (Program Graph) for Catch Exception and Catch IOException

I use the following partial code, where I the relevant line are enumerated by #number:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PS_5
{ 
    Class BinarySearch
    {
        Static void Main (string[] args)
        {
#2      int[] elemArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        .....

#15     StreamWriter sw = null;
            try 
            {
#16             sw = new StreamWriter ("output.txt")
#17.1, 17.2, 17.3               for (int i=0; i<elemArray.Length; i++)
                {
#18                 sw.Write(elemArray[i] + ",");
                }
#19         sw.WriteLine();
            }
#21         catch (IOException ex)
            {
#22             Console.WriteLine (ex.Message)
            }
#23         catch (Exception ex)
            {
#24             Console.WriteLine (ex.Message)
            }
        finally
        {
#25         sw.Flush();
#26         sw.Close();
        }
        }
    }
}

I would like to transfer this code into DAG (Program Graph). I would like to ask the following:

(1) Is there a dependency between line 17.3 (i++) to line 23 (catch (Exception ex) )? is there a way that line 23 will be executed after line 17.3? As far as I see, line 23 is aimed to catch an error for i which is an integer. In case of exceeding its maximal number there won't be an error since i will get its minimum value. So is there another situation that line 23 will be executed after line 17.3?

(2) I know that there is a dependency between line 18 to line 21 since in line 18 we use the Write command that is an IO and there might be an exception in IO. However the content of the Write command includes also elemArray[i]. Is there a chance that line 23 the regular catch Exception will run because of it?

(3) Is there any dependency between lines 21 and 23. Are they mutual exclusive? I mean if catch IOException works than the Catch Exception won't work and vise versa? Do they share the same stack for the executing the errors?

Upvotes: 0

Views: 104

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186748

Answers:

Q1. Technically, yes: you may have an integer overflow:

 int i = int.MaxValue;

 checked {
   i++; // <- integer overflow
 }

however, it's very unlike case.

Q2. Yes, technically, you can have out of range exception: if elemArray.Length == int.MaxValue then condition i<elemArray.Length is always true and

 int i = int.MaxValue;

 unchecked {
   i++; // <- no integer overflow (thanks to "uncheked"), i is negative now

   var x = elemArray[i]; // <- Out of range (since i is negative)
 }     

it's a very unlike case as well as the 1st one.

Q3. Yes, they are mutually exclusive either catch (IOException ex) is called either catch (Exception ex) or none of them.

A tip: you may find Linq being a better solution instead of that stuff with readers, streams:

  File.WriteAllText("output.txt", String.Join(",", lemArray));

Upvotes: 1

Related Questions