There is a bot for VC with the command "wiki". When there is an article in the wiki, the bot gives a link to it. When non-existent still gives the link, but with FileNotFoundException. What kind of condition must be made to prevent the exception and output a message like "article not found"?
@Override public String execute(IDialog dialog, String[] args, Bot bot) { String text = ObjectJoiner.join(" ", args); try { HttpURLConnection connection = (HttpURLConnection) new URL("https://ru.wikipedia.org/wiki/" + text).openConnection(); connection.setInstanceFollowRedirects(true); connection.connect(); connection.getInputStream().close(); return "Лови!<br>" + URLDecoder.decode(connection.getURL().toString(), "UTF8"); } catch (Exception e) { e.printStackTrace(); return e.getLocalizedMessage(); } } Exception when a non-existent article is as follows:
java.io.FileNotFoundException: https://ru.wikipedia.org/wiki/%D0%96%D0%B8%D1%80%D0%B8%D0%BA at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source) at ru.hilgert.cmds.commands.WikiCommand.execute(WikiCommand.java:45) at ru.hilgert.cmds.HBotCmds.dispathCommand(HBotCmds.java:254) at ru.hilgert.cmds.HBotCmds.handleCmd(HBotCmds.java:208) at ru.hilgert.cmds.HBotCmds.onCmd(HBotCmds.java:168) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at ru.hilgert.hbot.events.EventHandlerMethod.invoke(EventHandlerMethod.java:23) at ru.hilgert.hbot.plugin.PluginManager.fireEvent0(PluginManager.java:118) at ru.hilgert.hbot.plugin.PluginManager.fireEvent(PluginManager.java:110) at ru.hilgert.hbot.Bot$MessageConsumer$1$1.call(Bot.java:107) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)