投掷此示例的方法中的例外
我正在做一种使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否验证了您的例外可能会被抓住?如果被捕,代码将打印一个堆栈跟踪。但是,如果未抓住它,JVM将无论如何都会打印出堆栈跟踪。
因此,将异常带有一些消息,您可以验证,喜欢
并通过更多的详细信息来捕获异常:
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
and catch the exception by being a bit more verbose:
实际上,捕获了您的例外,您可以按以下方式进行检查:
程序的输出将为:
此输出意味着执行了catch块内部的代码。
Actually, your exception is caught, you can check it as follows:
The output of the program will be:
This output means that code inside catch block was executed.