0

Hi I wanted to know what the difference and similarities between the two variations of the kind of the same methods would be.

public string test(String value)
throw new testException();

and

public abstract String test(String value) throw new testException;

please forgive me if my syntax is wrong.

user5647516
  • 1,083
  • 1
  • 9
  • 19

2 Answers2

3
public abstract String test(String value) throw new testException;

does not make sense. The closest thing you can do is write

public abstract String test(String value) throws testException;

which indicates that test is a method that could throw a testException. If testException is not a RuntimeException, then it must be declared like that. But adding throws testException to the method signature only says that the method could throw that exception, it doesn't actually do the throwing.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
1

In java each method should indicate if they are throwing exceptions or not by using this syntax:

public String test(String value) throws testException
{
   //your code here
   //at some point you will throw the exception like this:
   throw new testException();
}

if you are using abstract method, the method signature only needs to contain the throws expression but when you implement the method you need to add the throw statement in the body of the method.

Pooya
  • 6,083
  • 3
  • 23
  • 43