I am trying to add a link to an “about” window in an application that will link to my Github. I sort of know the process, however, it seems to complicated for what it does, with all the error catching I have to put in. As I am certain the URI syntax is correct, is there any way to skip this catch? This variable stays final, and unless the syntax of resource location changes, it should be OK
Code:
//TODO: Simplify?
String gitLocation = "https://github.com/DanubeRS";
try {
final URI gitUri = new URI(gitLocation);
//Add the github button action (link to site)
githubButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Desktop.getDesktop().browse(gitUri.resolve(gitUri)); //Try to open URL
} catch (IOException e1) {
//URI unresolvable
e1.printStackTrace();
}
}
});
} catch (URISyntaxException e) {
e.printStackTrace();
}
You could use the convenience method URI.create(String str), which wraps the
URISyntaxExceptionwith anIllegalArgumentException. From the JavaDoc:The
IOExceptionthrown fromDesktop.browse()indicates that there’s no default browser set up. You should probably handle that gracefully by notifying the user.