-4

Why it happens? Here is my code to read dictionary from bin file

public static object LoadObject()
{
    try
    {
        if (File.Exists("D://my.bin"))
        {
            FileStream stream = File.OpenRead("D://my.bin");
            BinaryFormatter formatter = new BinaryFormatter();

            Dictionary<int, Question> deserializedObject = (Dictionary<int, Question>)formatter.Deserialize(stream);
            stream.Close();
            return deserializedObject;
        }
    }
    catch
    {

    }
}
dkozl
  • 32,814
  • 8
  • 87
  • 89
user3528837
  • 65
  • 1
  • 2
  • 10

4 Answers4

4

Your method is suppose to return an object of type object, but you are only returning inside the try block and that too from inside an if statement. So if your condition fails, your method will not return anything. Also if there is an exception in try block, before returning the object then your method would fail to return anything. That is why you are getting the error. You can fix that by returning null from outside of try / catch block.

public static object LoadObject()
{
    try
    {
        if (File.Exists("D://my.bin"))
        {
            FileStream stream = File.OpenRead("D://my.bin");
            BinaryFormatter formatter = new BinaryFormatter();

            Dictionary<int, Question> deserializedObject = (Dictionary<int, Question>)formatter.Deserialize(stream);
            stream.Close();
            return deserializedObject;
        }
    }
    catch(Exception ex)
    {

        //log exception
        return null;
    }
    return null;
}

Although this will will fix your error, but you should look for other option like throwing an exception if the file is not found, or returning an error object.

Habib
  • 219,104
  • 29
  • 407
  • 436
2

You need a return also if your code raises an exception or if the condition File.Exists is not met However, as is, there is no point in catching the exception. If you don't do anything with it, let it bubble at the upper level

public static object LoadObject()
{
    if (File.Exists("D://my.bin"))
    {
        FileStream stream = File.OpenRead("D://my.bin");
        BinaryFormatter formatter = new BinaryFormatter();

        Dictionary<int, Question> deserializedObject = (Dictionary<int, Question>)formatter.Deserialize(stream);
        stream.Close();
        return deserializedObject;
    }
    else
        throw FileNotFoundException("There is no file named", "D:\\my.bin");
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • the error is still the same – user3528837 Apr 21 '14 at 17:57
  • I would not just blindly return `null` just because I'm "lacking a better option." I would return `null` when I want to return `null`. If I'm "lacking a better option," I would look for the fundamental flaw in the design of my method. – Ant P Apr 21 '14 at 17:58
  • 1
    @AntP I disagree. Of course the best path is always to find the flaw in your design, but it is not prohibited to return null from a method. In this case you could also design this method to return null if the file doesn't exist. However I prefer to throw an exception and let the caller handle the situation – Steve Apr 21 '14 at 18:06
  • 1
    I didn't say it's prohibited - or even suggest it's bad - to return null from a method; it is, however, bad to return any value just because you're not sure what else to do. If there's no valid execution path, then throwing an exception - as you say - is the correct thing to do; not just return null and hope for the best. – Ant P Apr 21 '14 at 18:08
1

As the error message suggests, all the code paths must return a value. You have two conditions where no value is returned:

  1. If the file does not exist.
  2. If an exception is thrown.

Change the code to:

try
{
    if (File.Exists("D://my.bin"))
    {
       ...
       return deserializedObject;
    }
}
catch
{

}
return null; // Or return an empty dictionary with:
             // return new Dictionary<int, Question>();
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

You can try this

public static object LoadObject()
 {
   try
     {
       if (File.Exists("D://my.bin"))
         {
            FileStream stream = File.OpenRead("D://my.bin");
            BinaryFormatter formatter = new BinaryFormatter();

            Dictionary<int, Question> deserializedObject = (Dictionary<int, Question>)formatter.Deserialize(stream);
            stream.Close();
            return deserializedObject;
         }
     }
    catch
       {
         return null;
       }
     return null;
 }