# Visual Basic > Xamarin >  Using REFIT throws an error when first time used, how to capture the exception

## dee-u

First time to use Xamarin and I am trying to use a RESTful API for logging in, this code works but the first time I am clicking the BtnConnect it is throwing out an error and I am not sure how can I capture the error, I am using an android device connected thru USB, how can I capture or log the error?



```
private async void BtnConnect_Click(object sender, EventArgs e)
{
    try
    {
        txtUsername = FindViewById<TextView>(Resource.Id.txtUsername);
        txtPassword = FindViewById<TextView>(Resource.Id.txtPassword);

        string username = txtUsername.Text;
        string password = txtPassword.Text;
                
        var user = new UserModel
        {
            username = username,
            password = password
        };

        ApiResponse response = await ApiUtils.GetApiService().Login(user);

        Toast.MakeText(this, response.message, ToastLength.Long).Show();
    }
    catch (Exception ex)
    {
        Toast.MakeText(this, ex.StackTrace, ToastLength.Long).Show();

    }
}
```

----------


## PlausiblyDamp

> First time to use Xamarin and I am trying to use a RESTful API for logging in, this code works but the first time I am clicking the BtnConnect it is throwing out an error and I am not sure how can I capture the error, I am using an android device connected thru USB, how can I capture or log the error?
> 
> 
> 
> ```
> private async void BtnConnect_Click(object sender, EventArgs e)
> {
>     try
>     {
> ...


So what error is it throwing?

----------


## dee-u

Was overthinking this, I realized that I could log the exception in the Catch:

Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
Console.WriteLine(ex.StackTrace);
Toast.MakeText(this, ex.StackTrace, ToastLength.Long).Show();

This was the logged messages:




> 02-29 21:45:44.904 I/mono-stdout( 9934): An error occurred while sending the request.
> An error occurred while sending the request.
> The server returned an invalid or unrecognized response.
> 02-29 21:45:44.933 I/mono-stdout( 9934): The server returned an invalid or unrecognized response.
>   at System.Net.Http.HttpConnection.SendAsyncCore (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x012d9] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs:718 
> 02-29 21:45:45.111 I/mono-stdout( 9934):   at System.Net.Http.HttpConnection.SendAsyncCore (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x012d9] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs:718 
>   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync (System.Net.Http.HttpConnection connection, System.Net.Http.HttpRequestMessage request, System.Boolean doRequestAuth, System.Threading.CancellationToken cancellationToken) [0x000e6] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:316 
>   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync (System.Net.Http.HttpRequestMessage request, System.Boolean doRequestAuth, System.Threading.CancellationToken cancellationToken) [0x00101] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:296 
> 02-29 21:45:45.112 I/mono-stdout( 9934):   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync (System.Net.Http.HttpConnection connection, System.Net.Http.HttpRequestMessage request, System.Boolean doRequestAuth, System.Threading.CancellationToken cancellationToken) [0x000e6] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:316 
> ...


It only happens the first time I click the button, the succeeding clicks doesn't raise the above exception.

----------


## dee-u

I am using the Retry pattern as a "temporary" fix: https://docs.microsoft.com/en-us/azu...patterns/retry

It has been recommended in the following case study: https://4lowtherabbit.github.io/blogs/2018/11/CaseStudy

If anyone has other recommended solution then please hit the reply button, I am leaving this open for now.

----------

