RE: How to make test cases run even if an exception occurs. Exceution of cases should not stop.


My selenium scripts are getting stopped if an exception occurred and the remaining set of cases are not executing in that suite file .

I want the suite to execute the remaining cases also even if an exception occured.

Thanks in advance.

mallik9292 Train Asked on November 10, 2014 in Selenium WebDriver.
3 Answers

Hi Malik,

Below is the same test case, avoid the code but try to understand the try catch logic:

package testCases;
public class Framework_002 {
    public WebDriver driver;
    private String sTestCaseName;
    private int iTestCaseRow;
  public void beforeMethod() throws Exception {
          sTestCaseName = this.toString();
          sTestCaseName = Utils.getTestCaseName(this.toString());
        ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData,"Sheet1");
        iTestCaseRow = ExcelUtils.getRowContains(sTestCaseName,Constant.Col_TestCaseName);
        driver = Utils.OpenBrowser(iTestCaseRow);
        new BaseClass(driver);  
  public void f() throws Exception {
        ExcelUtils.setCellData("Pass", iTestCaseRow, Constant.Col_Result);
      }catch (Exception e){
          ExcelUtils.setCellData("Fail", iTestCaseRow, Constant.Col_Result);
          Utils.takeScreenshot(driver, sTestCaseName);
  public void afterMethod() {

So, in case any exception occured in my main test, the try catch block will catch the exception, make the logicall end of the test case and move on to the next test case. The only problem in the above code is that in the TestNG, it will display you as TEst Pass, but in the Excel result file it will say Test Fail. but again you can handle that with inserting one more logic or with listeners. Above is just for illustration purpose to give you an idea. Please do visit the chapter of Exception handling on

Lakshay Sharma

Professor Answered on November 10, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.