hello everyone. when working with objects, how would one specify that the input of negative numbers is not allowed in the constructor part of the code?

Dani AI

Generated

A few practical points that build on what and started discussing.

Prefer throwing a specific unchecked exception for invalid constructor arguments rather than throws Exception. Use IllegalArgumentException for precondition failures so callers are not forced into try/catch for programmer errors. If you want to avoid exceptions entirely, use a static factory that returns Optional to signal failure without throwing.

// constructor: fail-fast with unchecked exception
public CircleObj(int x, int y, int radius) {
    if (radius < 0) throw new IllegalArgumentException("radius must be >= 0");
    // assign fields...
}
// factory: return empty when inputs are invalid
public static Optional<CircleObj> create(int x, int y, int radius) {
    if (radius < 0) return Optional.empty();
    return Optional.of(new CircleObj(x, y, radius));
}

When a constructor throws, no object is created. That explains the "not initialized" symptom: code that assumes the object exists will hit problems (NPE later). Avoid empty catch blocks that swallow the error; either handle the invalid input (ask the user again, show a message, log and abort) or let the unchecked exception propagate to a place that can deal with it. Document preconditions in the constructor Javadoc and add unit tests for negative inputs.

Quick checklist:

  • Do not throw raw Exception; prefer IllegalArgumentException or a custom unchecked type.
  • Validate inputs before calling the constructor or use a factory that returns Optional.
  • If you catch an error, handle it explicitly (do not silently continue with a null reference).
  • Add Javadoc and unit tests so consumers know how the constructor behaves.

Recommended Answers

All 5 Replies

Since you can't return an error code from a constructor, you could throw an Exception if the parameter(s) are not acceptable:

class X {
   public X(int value) throws Exception {
      if (x<0) throw new Exception("Parameters must not be negative");
...

thanks! that helps with that problem...but now i am getting another error. on the object lines of code;

c1 = new CircleObj(inputx,inputy,inputradius);

it says "unhandled exception type Exception"

sorry if this is basic stuff, i am very new to this.

you are getting that error because in your constructor part you specified that an Exception is thrown therefor any calls to that exception must handle that thrown Exception.

To solve that problem surround your code with a try - catch clause

try{
     c1 = new CircleObj(inputx,inputy,inputradius);
}catch(Exception ex){
//put some error messages here or any error handlers
}

alright, but now it is saying that my variable c1 has not been initialized..

after i edit the code with the try catch

System.out.println("Results for Circle #1");
	System.out.println("X: "+c1.getX());
	System.out.println("Y: "+c1.getY());
	System.out.println("Radius: "+c1.getRadius());

declare your c1 variable outside the try catch and initialize it with a null value ..something like

CircleObj c1 = null;
      try{

      c1 = new CircleObj(inputx,inputy,inputradius);

      }catch(Exception ex){

      //put some error messages here or any error handlers

      }

Note: Anything you declare inside the try-catch is a local variable in that clause so if you declare c1 inside the try catch you can only use it inside that clause.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.