1

Possible Duplicate:
Throws or try-catch

I'm writing an API and I wish to write code such that I can raise an exception in a particular scenario. I've created an exception class as follows :-

public class InvalidSeverityException extends Exception {
    private static final long serialVersionUID = 1L;
    public InvalidSeverityException() {
        // TODO Auto-generated constructor stub
    }
}

In the codebase im having the following to call the code :-

throw new InvalidSeverityException();

However Eclipse suggests that I either use throws or enclose it within a try ... catch block. I feel that I shouldn't be catching this error and that the developers who use my API should enclose the code within try...catch.

Does that make sense? Am I doing something wrong?

Community
  • 1
  • 1
  • take a look at this on http://stackoverflow.com/questions/3203297/throws-or-try-catch – RNJ Dec 07 '12 at 16:34

4 Answers4

4

When handling with exceptions in Java you must understand the concept of checked exceptions and unchecked exceptions.

In your case currently you are defining a checked exception, maybe you want an unchecked one.

Here's a brief description about each one of the types:

Checked Exceptions

This exceptions must be part of the method's signature that raises them (or that invokes one method that does) or you must catch them with a try catch block and deal with the problem. Usually checked exceptions are used when there is something that can be done about the error and also when you want the developer to be aware that such error may occur and that has to be handled.

In java java.lang.Exception is a checked exception and all its subclasses will also be checked.

Unchecked Exceptions

This exceptions on the other hand don't need to make part of the method signature, nor you have to wrap methods that throw new in a try catch block. It's just expected that somewhere in the call stack there will be a try catch to handle it, otherwise if it reaches the JVM it will nicely dump you a stacktrace.

In java java.lang.RuntimeException is an unchecked exception and so are all its subclasses.

My opinion

If you are defining an API my suggestion is to use checked exceptions, this is mostly because you explicitly inform the developers using your API that such an exception might occur (so they can handle it anyway they want).

pabrantes
  • 2,161
  • 1
  • 28
  • 34
1

The method where you have throw new InvalidSeverityException(); should define throws InvalidSeverityException

Example:

  void yourMethod() throws InvalidSeverityException
    {
    ........//Some code
    throw new InvalidSeverityException();

    }
kosa
  • 65,990
  • 13
  • 130
  • 167
1

You are correct, you should not catch it. As suggested by eclipse, you should use throws so that the developers will know that your method potentially throws that exception and can then catch it.

.... method() throws YourException{
James Montagne
  • 77,516
  • 14
  • 110
  • 130
1

Well then surely you follow the first suggestion by Eclipse and set your method to throw the exception.

public void myMethod() throws InvalidSeverityException {
    //throw it somewhere in here so that other 
    //developer can catch it while calling your method
}
Jon Taylor
  • 7,865
  • 5
  • 30
  • 55