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?
shroomiin
0
Light Poster
Recommended Answers
Jump to PostSince 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"); ...
Jump to Postyou 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); …
All 5 Replies
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
shroomiin
0
Light Poster
Jocamps
14
Junior Poster
shroomiin
0
Light Poster
Jocamps
14
Junior Poster
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.