Bad java ctors

Started by
1 comment, last by Antheus 16 years, 2 months ago
I'm trying to find an *elegant* way of going about this... When using Java, let's say I have a Widget class:

public class Widget
{
    private int x;

    public Widget()
    {
        x = 0;
    }

    public Widget(int ex)
    {
        if(ex >= 0)
            x = ex;
        else ;
            // this implies the ctor was passed "invalid" integer value
    }
}


Let's say, for whatever reason, proper construction of a Widget involves passing its constructor a nonnegative integer value. There must be some way of error-checking and error-handling when constructors are passed invalid parameters. Let's say (even though this is sort of "bad Java") we want the default constructor (Widget()) to be the constructor that gets used whenever some other explicit-value constructor (such as Widget(int)) detects bad input. My guess would be to use code that looks like this, but then again I'm not sure what other (more refined) solutions exist:

public Widget(int ex)
{
    if(ex >= 0)
        x = ex;
    else Widget();
}


I guess what I'm really asking here is: how does one handle bad input passed to constructors?
Advertisement
You could have a factory style function:

class Widget{   public static Widget makeWidget( int x )   {        try        {            return new Widget(x);        }        catch( IllegalArgumentException e )        {            return new Widget();        }   }   protected Widget( int x )   {       if( x < 0 ) throw IllegalArgumentException();       this.x = x;   }   protected Widget()   {        x = 0;   }}
Quote:I guess what I'm really asking here is: how does one handle bad input passed to constructors?


1) Exceptions (preferred)
class Widget {  Widget(int ex)  {    if (ex < 0) throw new IllegalArgumentException("Foo");    x = ex;  }}


2) Assertions
class Widget {  Widget(int ex)   {    assert( x >= 0); // assertions can be disabled  }}


3) Forcing the values
class Widget {  Widget(int ex)  {    if(ex >= 0) {      x = ex;    } else {      x = 0;    }  }}


Factory would be another, but I wouldn't recommend it. It's simply not idiomatic Java.

With RAII in C++, people avoid exceptions like plague. In Java, exceptions are the only way to go.

What it comes down to is, what should happen when invalid value is passed. Do you wait fail-fast or fail-safe approach. Exceptions are used when handling input beyond your control. It shouldn't happen under normal use, but can happen.

Assertions are way of saying something won't happen. No ifs or buts.

Forcing the value is fail-safe, you ignore the problem. Things will work, but be careful - fail-fast is often better way. Ignoring errors unless you're sure of all the side-effects is a good way to spend hours tracking down a bug.

Your approach was right. If/else statement in constructor. Java doesn't believe in terse code, so get used to typing that same stanza over and over.

This topic is closed to new replies.

Advertisement