1

I have this method:

public String getNum(int x) throws Exception { if(x == 0) { throws new Exception("x cannot be 0"); } ... } 

now in my JUnit test, I try this

@Test(expected=Exception.class) public void testNum() { String x = getNum(0); } 

But Eclipse still wants to me to either add a throws declaration or surround with try catch for String x = getNum(0);. What did I do wrong? This test should pass since I expected an Exception when I pass in 0.

2 Answers 2

4

Just add a throws clause to the declaration of testNum?

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I am such as noob
Everybody is blind at times. With > 2k points here you cannot really be a noob.
4

Just add throws declaration which Java compiler requires for checked exceptions. In your case it is nothing more than declaration :)

1 Comment

Thank you very much. I am such as noob

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.