Handling an alert by catching an exception selenium

driver.findElement(By.id("alert")).click();

Alert alert = wait.until(alertIsPresent());
try {
//trying to send keys values to a non existing an alert box.

alert.sendKeys("hello");
} catch (ElementNotVisibleException e) {
System.out.println(e.getMessage());
} finally {
//finally handling the alert.
alert.accept();
}
}

No images available.