This is because of your try-catch statement. Your variable
data
is actually assigned, but not in the case when exception is thrown prior to assignment of the reference. In this case, the execution will reach the point of
return data
when it is not assigned, which is unacceptable.
The lame solution would be modification of the line of the declaration:
byte[] data = null;
Now it will compile and pass the runtime, but —
don't do it!
The really good solution would be: remove all the exception handling! Surprised? You are handling the exceptions too locally. You need to let it go. One rule about exceptions is: don't handle them often. You need to do it rarely in some point of code which I call "the points of competence", plus in one universal point way up the stack of each thread. In the case if the UI thread, such universal point should be the main event-oriented cycle. If this is
System.Windows.Forms
please see my past answers:
Error Logging and Screen Shot.[
^],
Catching an Exception[
^].
There is a fully analogous handling in WPF; if interested, please find it by yourself, or just ask me a follow-up question; I'll answer.
—SA