0

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; } } 
2
  • 3
    Because your function doesn't definitely return a value. if ((uint)ce.ErrorCode == 0x800A03EC) If this isn't true then your catch block doesn't return... Commented Sep 30, 2014 at 16:08
  • possible duplicate of not all code paths return a value Commented Sep 30, 2014 at 16:11

2 Answers 2

4

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; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Better to just call throw (without the ce). If you include the ce, you will possibly lose some stack trace information.
1

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. } 

2 Comments

Get Exception from HRESULT: 0x80210015
It's better to just call throw rather than throw ce so you don't lose any stack trace information.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.