title: How do you catch CancellationToken.Register callback exceptions?
body: <p>I am using async I/O to communicate with an HID device, and I would like to throw a catchable exception when there is a timeout.  I've got the following read method:</p>
  
  <pre><code>public async Task&lt;int&gt; Read( byte[] buffer, int? size=null )
  {
      size = size ?? buffer.Length;
  
      using( var cts = new CancellationTokenSource() )
      {
          cts.CancelAfter( 1000 );
          cts.Token.Register( () =&gt; { throw new TimeoutException( "read timeout" ); }, true );
          try
          {
              var t =  stream.ReadAsync( buffer, 0, size.Value, cts.Token );
              await t;
              return t.Result;
          }
          catch( Exception ex )
          {
              Debug.WriteLine( "exception" );
              return 0;
          }
      }
  }
  </code></pre>
  
  <p>The exception thrown from the Token's callback is not caught by any try/catch blocks and I'm not sure why.  I assumed it would be thrown at the await, but it is not.  Is there a way to catch this exception (or make it catchable by the caller of Read())?</p>
  
  <p><strong>EDIT:</strong>
  So I re-read the doc at <a href="http://msdn.microsoft.com/en-us/library/dd321635%28v=vs.110%29.aspx" rel="noreferrer">msdn</a>, and it says "Any exception the delegate generates will be propagated out of this method call."</p>
  
  <p>I'm not sure what it means by "propagated out of this method call", because even if I move the .Register() call into the try block the exception is still not caught.</p>