1

I'm returning to Java (Android) after a long time away in C# .NET -land, and trying to remember the various idiosyncrasies.

One that's got me stuck is this; I'm throwing an Exception in my constructor if my class fails to initialise properly, as follows:

    // Constructor
    public CountdownDictionary(JSONArray jsValues, ArrayList<String> returnFieldList)
{
    this.DictionaryType = getDictionaryTypeFromValues(jsValues);
    if (this.DictionaryType == CountdownDictionaryTypes.Unknown)
    {
        throw new Exception("Unknown dictionary type");
    }
}

Eclipse give a compile-time error

Unhandled exception type Exception

This is confusing me; since how can I handle the exception? I don't want to handle the Exception in this class, I want it to be unhandled so that it bubbles up to the caller and they can handle it!

What am I missing?

Carlos P
  • 3,928
  • 2
  • 34
  • 50

9 Answers9

4

You are throwing an exception inside constructor, but didn't specify throws clause in method signature, which is compile time error, that is why eclipse complaining. Following code snippet will resolve the issue.

 public CountdownDictionary(JSONArray jsValues, ArrayList<String> returnFieldList) throws Exception
kosa
  • 65,990
  • 13
  • 130
  • 167
  • Thanks, think you were first on this. Geez, so I have to add the 'throws' declaration to every method in the callstack until the point where I want to handle it? :-| Oh well. – Carlos P Jul 10 '12 at 18:45
  • Unless you make it a RuntimeException, which is the C# default. Then you don't have to add the throws clause. – duffymo Jul 10 '12 at 19:22
2

Add throws clause to the header of the method constructor:

public CountdownDictionary(JSONArray jsValues, ArrayList<String> returnFieldList) throws Exception
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

You need to indicate the compiler your constructor can throw an Exception by appending throws Exception to your method signature, thus any callers know they will have to handle it.

jhurtado
  • 8,587
  • 1
  • 23
  • 34
2

The other option is to throw a runtime exception instead of a checked exception - though this depends on whether this exception would only be thrown due to a programmer error.

If it is not a programmer error Its generally a bad call to throw an exception in a constructor. It's also generally a bad call to only throw an exception of the type Exception. This is because you should provide application specific behaviour at a higher level to handle this bad behaviour.

plasma147
  • 2,191
  • 21
  • 35
  • I agree with you about throwing an Exception; this is just prototype code and I'll be subclassing Exception to more closely define the problem. On the issue of throwing Exceptions in constructors, this is more open to debate, e.g. http://stackoverflow.com/questions/1371369/can-constructors-throw-exceptions-in-java – Carlos P Jul 10 '12 at 18:46
1

Just add throws Exception before your opening brace.

antlersoft
  • 14,636
  • 4
  • 35
  • 55
1

If you want the Exception to be handled by the calling method, use this:

public CountdownDictionary(JSONArray jsValues, ArrayList<String> returnFieldList) throws Exception {
    // ...
}

That way, this method is shown to throw an Exception and force any calling code to handle it.

Cat
  • 66,919
  • 24
  • 133
  • 141
1

The following line: throw new Exception("Unknown dictionary type"); is throwing an exception which is not being caught anywhere in your program.

Instead, do :

try {
    throw new Exception("Unknown dictionary type");
}catch(Exception e) {
    e.printStackTrace();
}

Or

public CountdownDictionary(JSONArray jsValues, ArrayList<String> returnFieldList) throws Exception 
Chander Shivdasani
  • 9,878
  • 20
  • 76
  • 107
0

Try this...

I normally use this and remember this as following:

Combination of throws and throw.

The method throws bad exception.

And inside the condition, its throw new exception object.

Eg:

public CountdownDictionary(JSONArray jsValues, ArrayList<String> returnFieldList) throws Exception
{
    this.DictionaryType = getDictionaryTypeFromValues(jsValues);
    if (this.DictionaryType == CountdownDictionaryTypes.Unknown)
    {
        throw new Exception("Unknown dictionary type");
    }
}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

I'd do it like this:

// Constructor
public CountdownDictionary(JSONArray jsValues, List<String> returnFieldList)
{
    if (jsValues == null) {
        throw new IllegalArgumentException("JSON array cannot be null");
    }
    if (returnFieldList == null) {
        throw new IllegalArgumentException("Return field list cannot be null");
    }
    this.DictionaryType = getDictionaryTypeFromValues(jsValues);
    if (this.DictionaryType == CountdownDictionaryTypes.Unknown)
    {
        throw new IllegalArgumentException("Unknown dictionary type");
    }
}

No throws clause needed, because it's an unchecked exception.

duffymo
  • 305,152
  • 44
  • 369
  • 561