-3

pls find the code below:

public static void selectDefinition(String defName)
        {
           driver.findElement(By.xpath("//table[@id='MainContent_gdvDefs_DXMainTable']//td[text()='"+defName+"']")).click();
        }

and

  try{
        selectDefinition(defdelname);
        System.out.println("Definition "+defdelname+" was not removed from the table");
    }
    catch (Exception ex)
    { 
        System.out.println("Definition "+defdelname+"was removed successfully from the table");

    }

in the above code if the "defdelname" is deleted the catch block is not executing but for selectDefinition it is throwing no such element exception.

i am a beginner pls help me out...to solve this issue i want the catch block to be executed any workaround for that?

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55

1 Answers1

0

You have to add throws Exception to your method, in this way the exception is thrown to the calle inside the try/catch block and correctly handled:

public static void selectDefinition(String defName) throws Exception
        {
           driver.findElement(By.xpath("//table[@id='MainContent_gdvDefs_DXMainTable']//td[text()='"+defName+"']")).click();
        }

This is NOT how exceptions are meant to work. They are used to alert that an error has occured during the execution of your program and not to manage your program workflow. Also in your code you are using the catch block to report a successful status, the opposite of what an exception catch is for.

If your method can de both succesfull and not (it is a possible outcome to remove and to not being capable of remove from the table, according to your example) you should use a return statement with a meaningful value, such as a boolean true \ false variable. Exception should occur only if the behavior that created it was NOT meant to occur.

Narmer
  • 1,414
  • 7
  • 16