mr im
mr im

Reputation: 1

Try Catch Exceptions Error

When I use try catch exception with this piece of code, I get the following error:

"not all code paths return values"

My code:

   public System.Drawing.Image Scan()
    {
        try
        {

           const string formatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
            WIA.CommonDialog scanDialog = new WIA.CommonDialog();
            WIA.ImageFile imageFile = null;
            imageFile = scanDialog.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType, WIA.WiaImageIntent.GrayscaleIntent,

            WIA.WiaImageBias.MinimizeSize, formatJPEG, false, true, false);
            WIA.Vector vector = imageFile.FileData;
            System.Drawing.Image i = System.Drawing.Image.FromStream(new System.IO.MemoryStream((byte[])vector.get_BinaryData()));

            return i;

        }
        catch (COMException ce)
        {
            if ((uint)ce.ErrorCode == 0x800A03EC)
            {
               return ce;

            }
        }

Upvotes: 0

Views: 195

Answers (2)

Vimal CK
Vimal CK

Reputation: 3563

Change your catch block like below will work but still you face some issue. Because your method returns type Image and you are returning COMException in catch block. I suggest you to throw the exception or Log in catch block

if ((uint)ce.ErrorCode == 0x800A03EC)
{
    //DO LOGGING;
}
else
{
    throw ce;
}

Upvotes: 4

StuffOfInterest
StuffOfInterest

Reputation: 518

You have two different problems here. First, your catch block won't return anything if the condition is not met. Second, the return type within the catch block is not the same as the one inside of the try block.

You probably want something more like this in your catch block:

catch (COMException ce)
{
    if ((uint)ce.ErrorCode == 0x800A03EC)
        return null;  // Don't return anything if a specific code was found

    throw ce;         // Rethrow the exception for all other issues.
}

Upvotes: 1

Related Questions