【问题标题】:Why is my code not throwing me an error?为什么我的代码没有给我一个错误?
【发布时间】:2016-09-18 20:56:57
【问题描述】:

我目前正在探索我的第一个第三方库,一切正常,除了它应该在出现错误时向我抛出错误,而它没有..

这是我的代码,我已经尝试过导致错误,但它应该会导致错误,但事实并非如此,应用程序仍在运行

例如,当我从消费者密钥中删除一个字母或数字时,它没有连接,它应该给我一个错误。

我正在查看文档并尝试遵循此操作

Destroying a Friendship

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tweetinvi;
using Tweetinvi.Core.Authentication;
using Tweetinvi.Core;
using Tweetinvi.Credentials;
using Tweetinvi.WebLogic;

namespace Tweetinvi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string accessToken;
        string accessTokenSecret;
        string consumerKey;
        string consumerSecret;
        string hashTag;
        string userName;



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)

        {
            try
            {
                Auth.SetUserCredentials(consumerKeyBox.Text, consumerSecretBox.Text, accessTokenBox.Text, accessTokenSecretBox.Text);
                Tweet.PublishTweet("Hello World :)");
                //User.UnFollowUser("");
            }
            catch(Exception ex)
            {
                MessageBox.Show("Something went wrong");
            }



        }
    }
}

【问题讨论】:

  • 应该出了什么问题?
  • 您是在Release 模式还是Debug 模式下运行程序?
  • 在使用第三方库时,一定要阅读documentation以确认您的期望与现实。默认情况下,TweetInvi 会吞下异常。这应该由返回包含异常的nullExceptionHandler.GetLastException()Tweet.PublishTweet() 指示。

标签: c# .net linq twitter tweetinvi


【解决方案1】:

它没有抛出异常,因为没有要抛出的异常。

当您从 twitter 发布推​​文时,它应该返回一个您可以检查的对象。如果 publishTweet() 方法失败,它可能不会抛出异常,而是返回一个响应对象。

检查该对象并进行相应处理。

【讨论】:

    【解决方案2】:

    我是 Tweetinvi 的开发人员,为了清楚起见,这里是 Tweetinvi 的工作原理。

    • Twitter 抛出的WebException 默认由ExceptionHandler 处理。这种功能的想法是大多数开发人员不关心为什么失败,而只关心是否失败。在这种情况下,您将需要使用ExceptionHandler,它是一个异常记录器。当异常失败时,Tweetinvi 将根据 Twitter 端点返回 null 对象或 false。

    • 您可以使用以下代码行禁用ExceptionHandlerExceptionHandler.SwallowWebExceptions = false;。这样做时,您必须在 Tweetinvi 类 Tweetinvi.Core.Exceptions.TwitterException 上使用 try/catch。您可能还想处理以下异常:

      • TwitterTimeoutExceptionTweetinviConfig.ApplicationSettings.HttpRequestTimeout 指定的异常已超时。
      • TwitterNullCredentialsExceptionTwitterInvalidCredentialsExceptionAuth.SetUserCredentials() 设置。
    • ArgumentExceptionArgumentNullException 如果您指定的参数之一不正确,将始终被抛出。在下一个版本 0.9.13.0 中,我们将改进此类异常 Find more 的细节。

    您可以在此处找到用于在 Tweetinvi 中处理异常的 wiki:

    https://github.com/linvi/tweetinvi/wiki/Exception-Handling

    【讨论】:

      【解决方案3】:

      试试这个代码,看看你的用户是否通过了身份验证。如果不是,那么一定是权限或者你的key是错误的:

      ITwitterCredentials creds = new TwitterCredentials(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
      Auth.SetCredentials(creds);
      var authenticatedUser = User.GetAuthenticatedUser(creds);
      

      【讨论】:

        猜你喜欢
        • 2021-06-09
        • 1970-01-01
        • 2017-05-28
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        • 2012-07-28
        • 1970-01-01
        • 2019-08-19
        相关资源
        最近更新 更多