Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

When we try to access some resources in the network, we must first obtain the resource’s Uniform Resource Identifier (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.

Let’s say we have the address string of some resource in the network. There are two ways to get a URI instance:

  • 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

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:

assertThrows(URISyntaxException.class, () -> new URI("I am an invalid URI string."));

We should note that URISyntaxException is a subclass of 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:

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.

As we can see, creating a URI instance using the constructor is pretty straightforward. Next, let’s move to the URI.create() method.

3. Using the URI.create() Method

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:

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. 

So next, let’s create a test to see if we can get the expected IllegalArgumentException, if we feed create() an invalid URI string:

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:

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:

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).

If we don’t want to deal with the checked exception, we can use the create() static method to create the URI instance.

5. Conclusion

In this article, we’ve discussed the difference between instantiating a URI object using the constructor and the URI.create() method.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

2 Comments
Oldest
Newest