0

I am accessing thousands of URLs via Java Selenium. I think some of these are 404s or 500. I want to if the response is other than 200 than throw an exception.

I know Jmeter can do it, I also know it's a bad practice to throw exceptions like this, but due to some limitations in the framework, it can only report exception in final reports. We are capturing exception in JUnit's @After block

What I tried is this, but it doesn't throw an exception. What am I missing in this code?

    if (getDriver().getTitle().contains("404")) {
        syso("throw exception, page is 404");
        try {
            throw new MalformedURLException("page not found");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        consoleLogs("Page found ...");
    }

I think it is not throwing exception because I have handled it. But if I don't handle it gives compilation error.

Please help me to fix this.

paul
  • 4,333
  • 16
  • 71
  • 144
  • 2
    The compilation error probably says the exception must be caught or **declared to be thrown**. So you want to declare it thrown, not to catch it. – khelwood Aug 08 '19 at 14:22
  • You will need to add something like method() throws Exception{} https://stackoverflow.com/questions/37424284/unreported-exception-java-lang-exception-must-be-caught-or-declared-to-be-throw/37424329 – Rahul L Aug 08 '19 at 14:26
  • Selenium might not be the tool you want to use for this job. However you could use browsermob proxy to more easily capture the request data (including response) – DMart Aug 08 '19 at 18:41
  • malformed url's are not 404's – Corey Goldberg Aug 11 '19 at 14:39

1 Answers1

0

The exception is being thrown but your code catches it right after.

Remove the try/catch block and change your method's signature like this:

public void methodName() throws MalformedURLException {}