投掷此示例的方法中的例外

发布于 2025-01-28 05:19:05 字数 1868 浏览 3 评论 0原文

我正在做一种使用Mojang API从他的用户名中返回Minecraft播放器的UUID的方法。此方法在参数(我们想了解UUID的玩家的用户名)中采用字符串。要使用API​​的结果,我使用SimpleJson库(将JSON结果解析为字符串以返回)。

我的方法抛出2个检查了异常:ioexection和parseException,因为我想要。 当一个错误的用户名(因此不存在的用户名)api返回一个空的json对象,而我的方法在这种情况下会引发ioexception。这是我的问题,当方法在方法中是错误的用户名时,该方法会抛出新的ioexcpetion,但是尝试捕捉该方法时,投掷例外并未捕获。

我的方法:

public static String getUUID(String name) throws IOException, ParseException {
        URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + name);
        URLConnection uc = url.openConnection();
        BufferedReader bf = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        StringBuilder response = new StringBuilder();
        String inputLine;
        while ((inputLine = bf.readLine()) != null) {
            response.append(inputLine);
        }
        bf.close();

        if (response.toString().isEmpty()) {
            throw new IOException();
        }
        JSONParser parser = new JSONParser();
        Object object = parser.parse(response.toString());
        JSONObject jo = (JSONObject) object;
        String str = (String) jo.get("id");
        return str.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5");
    }

使用有效用户名的示例:

public static void main(String[] args) {
        try {
            System.out.println(getUUID("Jeb_"));
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }

现在是一个错误的用户名:

public static void main(String[] args) {
        try {
            System.out.println(getUUID("d"));
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }

谢谢。

I am doing a method who return an UUID of a Minecraft player from his username using the Mojang API. This method takes a String in parameter (the username of the player who we want to know the UUID). To use the resultat of the API, I use the SimpleJSON library (to parse the JSON result into a String to return).

My method throws 2 checked exceptions : the IOExeption and the Parseexception, cause I want.
When a wrong username (so an username who doesn't exist) the API return a empty JSON object and my method throws an IOException in this case. And this is my problem, when a wrong username is in paramter of the method, the method throw a new IOExcpetion but with a try and catch for the method, the throwing exception isn't catched.

My method :

public static String getUUID(String name) throws IOException, ParseException {
        URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + name);
        URLConnection uc = url.openConnection();
        BufferedReader bf = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        StringBuilder response = new StringBuilder();
        String inputLine;
        while ((inputLine = bf.readLine()) != null) {
            response.append(inputLine);
        }
        bf.close();

        if (response.toString().isEmpty()) {
            throw new IOException();
        }
        JSONParser parser = new JSONParser();
        Object object = parser.parse(response.toString());
        JSONObject jo = (JSONObject) object;
        String str = (String) jo.get("id");
        return str.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5");
    }

An example of using a valid username :

public static void main(String[] args) {
        try {
            System.out.println(getUUID("Jeb_"));
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }

And now an example with a wrong username:

public static void main(String[] args) {
        try {
            System.out.println(getUUID("d"));
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠魁 2025-02-04 05:19:05

您是否验证了您的例外可能会被抓住?如果被捕,代码将打印一个堆栈跟踪。但是,如果未抓住它,JVM将无论如何都会打印出堆栈跟踪。

因此,将异常带有一些消息,您可以验证,喜欢

throw new IOException("Invalid user");

并通过更多的详细信息来捕获异常:

catch (IOException | ParseException e) {
    System.out.println("Could not lookup user "+username+", caught "+e.getClass().getName()+": "+e.getMessage());
}

Have you verified that your exception might get caught? If it is caught, the code prints a stack trace. But if it is not caught, the JVM will print a stack trace anyway.

So throw the exception with some message you can verify, like

throw new IOException("Invalid user");

and catch the exception by being a bit more verbose:

catch (IOException | ParseException e) {
    System.out.println("Could not lookup user "+username+", caught "+e.getClass().getName()+": "+e.getMessage());
}
标点 2025-02-04 05:19:05

实际上,捕获了您的例外,您可以按以下方式进行检查:

public static void main(String[] args) {
        var username = "d";
        try {
            System.out.println(getUUID(username));
        } catch (IOException | ParseException e) {
            System.out.println("User " + username + " not found!");
            e.printStackTrace();
        }
    }

程序的输出将为:

User d not found!
java.io.IOException
    at com.company.Main.getUUID(Main.java:37)
    at com.company.Main.main(Main.java:17)

此输出意味着执行了catch块内部的代码。

Actually, your exception is caught, you can check it as follows:

public static void main(String[] args) {
        var username = "d";
        try {
            System.out.println(getUUID(username));
        } catch (IOException | ParseException e) {
            System.out.println("User " + username + " not found!");
            e.printStackTrace();
        }
    }

The output of the program will be:

User d not found!
java.io.IOException
    at com.company.Main.getUUID(Main.java:37)
    at com.company.Main.main(Main.java:17)

This output means that code inside catch block was executed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文