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.

Train Asked on November 10, 2014 in Selenium WebDriver.
  • 3 Answer(s)

    Hi Malik

    Are your test cases getting skipped or all the test cases getting failed with same exception

    Below answer is if your remaining test cases are getting skipped
    In testng skip happens mostly due to 2 reason

    1. dependent methods parent method fails
    2. Exception is thrown in Configuration Method(@BeforeMethod,@BeforeTest,@BeforeClass, @After …..)

    I believe exception is thrown in some of the configuration method , as suggested by other do use try/catch atleast in configuration method,

    Supporter Answered on November 10, 2014.
    Add Comment

    Hi Malik

    You Need to Handle Those Exceptions so that Execution  of of Test Cases Wont Stop

    Example:: You have 2 test cases signup and login

    public test_signup()
    // code which is throwing exception

    }catch(exception e)
    // do nothing

    In this Way Exception causes by test_signup wont effect other test cases but make sure test case should not dependent on each  other


    Vice Professor Answered on November 10, 2014.

    Hi Ankit,

    Thanks for the reply.  I am executing some of the testcases like validating the page with different inputs  in a for loop at that time my cases are dependent so what can i do at that point

    on November 10, 2014.

    here is a basic example to show you how we deal with exception

    public class add
    public int number1()
    int a =10;
    return a;
    public int number2()           throws NullPointerException                                                                                    // now i know this method will return null pointer exception as we havent assigned any value to b
    int b;
    return b;
    }catch(NullpointerException e)
    b=10;    // now even if exception occurs the code will not stop exceuting as value of b is assigned so it can be used anywhere
    public void add(){           // this method will exceute even if exception in 2nd method
    int c;

    i just hope this will clear your concept

    on November 11, 2014.
    Add Comment

    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.