The javadoc for URL
's .toURI()
method mentions that it can throw a URISyntaxException
if:
[...] this URL is not formatted strictly according to to RFC2396 and cannot be converted to a URI.
However, I have so far unsuccessfully tried to trigger this exception. What is more, you need to "pass" the URL
constructor tests anyway.
So, does there exist an example of a valid URL
which is not a valid URI
?
The URL
class is particularly forgiving when it comes to tolerating syntactically incorrect URLs. If I remember correctly, it only validates if there is a known protocol handler for the scheme and accepts everything in the scheme specific part.
The easiest way to produce a URISyntaxException
is probably something like this:
new URL("http:// ").toURI();
It may contain any of the following characters:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=.
Any other character needs to be encoded with the percent-encoding (%hh). Each part of the URI has further restrictions about what characters need to be represented by an percent-encoded word.
It is also true that different characters are legal at different points. For example, according to RFC 2396, an unescaped '?' is legal in the fragment part but not the path part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With