5 Replies - 2485 Views - Last Post: 08 October 2012 - 08:33 AM Rate Topic: -----

#1 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

How to set-up this section of a constructor

Posted 08 October 2012 - 07:02 AM

I have an assignment that requires me to design a domain class, the private fields are start, end and delta. I am told to make a constructor and the constructor must check if delta may never be equal to 0, if the start is smaller than the end then the delta has to be positive, and if the start is bigger than the end then the delta has to be negative and that the constructor is to check if the delta is in the valid domain and if not an argument exception should be thrown. I am not sure how to set this up (the exception part), this is what I am assuming but it just doesnt make sense to me

class Domain
{
	private double start;
	private double end;
	private double delta;
	
	public Domain(double s, double e, double d)
	{
		start = s;
		end = e;
		delta = s;
		
		try
		{
			if (delta == 0)
			throw new ArgumentException();
		}
		catch (ArgumentException e)
		{
			Console.WriteLine(e.Message);
		}
		
		try
		{
			if (start < end && delta < 0)
			throw new ArgumentException();
		}
		catch (ArgumentException e)
		{
			Console.WriteLine(e.Message);
		}
		try
		{
			if (start > end && delta > 0)
			throw new ArgumentException();
		}
		catch (ArgumentException e)
		{
			Console.WriteLine(e.Message);
		}
	}




I really don't think this is the right interpitation of what the question is asking, where am I going wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: How to set-up this section of a constructor

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: How to set-up this section of a constructor

Posted 08 October 2012 - 07:45 AM

I don't know why you want to catch the exception in your constructor, don't you want to throw it so the calling part of your program can catch it :) ?

        public Domain(double s, double e, double d) {
            start = s;
            end = e;
            delta = s;

            if (delta == 0)
                throw new ArgumentException();
            if (start < end && delta < 0)
                throw new ArgumentException();
            if (start > end && delta > 0)
                throw new ArgumentException();
        }


Was This Post Helpful? 0
  • +
  • -

#3 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: How to set-up this section of a constructor

Posted 08 October 2012 - 07:58 AM

In the question it says the constructor checks if the delta is valid for the domain. If not, an argument exception should be thrown. If they are thrown in the constrcutor shouldnt the catch also be in the constructor too in this case??
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How to set-up this section of a constructor

Posted 08 October 2012 - 08:30 AM

No, if the exception is caught in the constructor, how will the calling code know it has to fix anything? I can't think of any situation where you want to throw and exception and catch it in the same code. Using exceptions as flow control is bad :)

This post has been edited by Momerath: 08 October 2012 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 313
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: How to set-up this section of a constructor

Posted 08 October 2012 - 08:31 AM

Nope. If your constructor is throwing an exception, the code that called the constructor should catch it, not the constructor itself.

Say if you ordered something from Amazon, and that order knew it'd failed, but didn't notify you, wouldn't you just sit around waiting for it? But if you were notified, you could retry, go somewhere else, etc? Think about the services your classes are providing for each other.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: How to set-up this section of a constructor

Posted 08 October 2012 - 08:33 AM

The point of throwing the exception is so that your caller knows that something very wrong has happened. By catching the exception within the same method/constructor you are masking the problem, as well as inappropriately using exceptions for flow control. If you want flow control, use the appropriate constructs: if-else, switch, while-continue-break, etc.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1