I am pressing a cancel button than according to my code it is checking some text. In Chrome and Firefox it is working fine but in IE it is taking time to perform operation
You want to wait until the alert is present before switching to it, similar to your 3rd row. Reference.
EDIT: try:
new WebDriverWait(driver, 60)
.ignoring(NoAlertPresentException.class)
.until(ExpectedConditions.alertIsPresent());
Alert al = driver.switchTo().alert();
al.accept();