1. Overview
1.概述
When we try to access some resources in the network, we must first obtain the resource’s Uniform Resource Identifier (URI).
当我们试图访问网络中的一些资源时,我们必须首先获得该资源的统一资源标识符(URI)。
The Java standard library provides the URI class, allowing us to handle URIs more easily. Of course, to use the URI class, the first step is to get a URI instance.
Java标准库提供了URI类,使我们能够更容易地处理URIs。当然,要使用URI类,第一步是要得到一个URI实例。
Let’s say we have the address string of some resource in the network. There are two ways to get a URI instance:
假设我们有网络中某种资源的地址字符串。有两种方法可以得到一个URI实例。
- calling the constructor with the address string directly – URI myUri = new URI(theAddress);
- calling the URI.create() static method – URI myUri = URI.create(theAddress);
In this quick tutorial, we’ll take a closer look at these two approaches and discuss their differences.
在这个快速教程中,我们将仔细看看这两种方法,并讨论它们的区别。
2. Using the URI Constructor
2.使用URI构造函数
First, let’s have a look at the constructor’s signature:
首先,让我们看一下构造函数的签名。
public URI(String str) throws URISyntaxException
As we can see, calling the constructor with an invalid address may raise URISyntaxException. To make it simple, let’s create a unit test to see this case:
我们可以看到,用无效的地址调用构造函数可能会引发URISyntaxException。为了简单起见,让我们创建一个单元测试来看看这种情况。
assertThrows(URISyntaxException.class, () -> new URI("I am an invalid URI string."));
We should note that URISyntaxException is a subclass of Exception:
我们应该注意,URISyntaxException是Exception的一个子类。
public class URISyntaxException extends Exception { ... }
Therefore, it’s a checked exception. In other words, when we call this constructor, we must handle the URISyntaxException:
因此,它是一个checked exception。换句话说,当我们调用这个构造函数时,我们必须处理URISyntaxException。
try {
URI myUri = new URI("https://www.baeldung.com/articles");
assertNotNull(myUri);
} catch (URISyntaxException e) {
fail();
}
We’ve seen the constructor usage and the exception handling as a unit test. If we execute the test, it passes. However, the code doesn’t compile without the try-catch.
我们已经看到了构造函数的用法和异常处理作为一个单元测试。如果我们执行测试,它通过了。然而,如果没有try-catch,代码就不能编译。
As we can see, creating a URI instance using the constructor is pretty straightforward. Next, let’s move to the URI.create() method.
正如我们所看到的,使用构造函数创建一个URI实例是非常直接的。接下来,让我们转到URI.create()方法。
3. Using the URI.create() Method
3.使用URI.create()方法
We’ve mentioned URI.create() can create a URI instance as well. To understand the difference between the create() method and the constructor, let’s look at the create() method’s source code:
我们已经提到URI.create()也可以创建一个URI实例。为了理解create()方法和构造函数之间的区别,让我们看一下create()方法的源代码。
public static URI create(String str) {
try {
return new URI(str);
} catch (URISyntaxException x) {
throw new IllegalArgumentException(x.getMessage(), x);
}
}
As we can see in the code above, the create() method’s implementation is pretty simple. First, it invokes the constructor directly. Further, if URISyntaxException is thrown, it wraps the exception in a new IllegalArgumentException.
正如我们在上面的代码中看到的,create()方法的实现相当简单。首先,它直接调用构造函数。此外,如果URISyntaxException被抛出,它将该异常包装成一个新的IllegalArgumentException。
So next, let’s create a test to see if we can get the expected IllegalArgumentException, if we feed create() an invalid URI string:
所以接下来,让我们创建一个测试,看看如果我们给create()一个无效的URI字符串,我们是否能得到预期的IllegalArgumentException。
assertThrows(IllegalArgumentException.class, () -> URI.create("I am an invalid URI string."));
The test passes if we give it a run.
如果我们让它运行一下,测试就会通过。
If we take a closer look at the IllegalArgumentException class, we can see it’s a subclass of RuntimeException:
如果我们仔细看看IllegalArgumentException类,我们可以看到它是RuntimeException的一个子类。
public class IllegalArgumentException extends RuntimeException { ... }
We know that RuntimeException is an unchecked exception. This means when we create a URI instance using the create() method, we don’t have to handle the exception in an explicit try-catch:
我们知道,RuntimeException是一个未检查的异常。这意味着当我们使用create()方法创建一个URI实例时,我们不需要在明确的try-catch中处理这个异常。
URI myUri = URI.create("https://www.baeldung.com/articles");
assertNotNull(myUri);
Therefore, the main difference between create() and the URI() constructor is the create() method changes the constructor’s checked exception (URISyntaxException) into an unchecked exception (IllegalArgumentException).
因此,create()和URI()构造函数之间的主要区别是create()方法将构造函数的检查异常(URISyntaxException)改为未检查的异常(IllegalArgumentException)。
If we don’t want to deal with the checked exception, we can use the create() static method to create the URI instance.
如果我们不想处理检查过的异常,我们可以使用create() static方法来创建URI实例。
5. Conclusion
5.总结
In this article, we’ve discussed the difference between instantiating a URI object using the constructor and the URI.create() method.
在这篇文章中,我们讨论了使用构造函数实例化一个URI对象和URI.create()方法之间的区别。
As usual, all code snippets presented here are available over on GitHub.
像往常一样,这里介绍的所有代码片段都可以在GitHub上找到。。