473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to throw an exception?

Tyler Wiebe
This is my first time using exceptions, and I can't seem to make the exception get thrown. This is meant for painting this shape on the form. If I have the curve height more then the height of the object, all my form does is creates red X's for all of the controls.

So what am I doing wrong?

P.S. It works fine if the curve height is less then the object height.

Expand|Select|Wrap|Line Numbers
  1.     public class BottomCurve
  2.     {
  3.         public BottomCurve(int Width, int Height, int CurveHeight)
  4.         {
  5.             if (CurveHeight >= Height) throw new System.Exception("The curve height cannot be greater then or equal to the height of this object");
  6.  
  7.             this.ISize = new System.Drawing.Size(Width, Height);
  8.  
  9.             this.ICurve = new System.Drawing.Point[]
  10.             {
  11.                 new System.Drawing.Point(0, this.Height),
  12.                 new System.Drawing.Point(this.Width / 2, this.Height - CurveHeight),
  13.                 new System.Drawing.Point(this.Width, this.Height)
  14.             };
  15.  
  16.             this.IGraphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
  17.             this.IGraphicsPath.StartFigure();
  18.  
  19.             this.IGraphicsPath.AddLine(0, 0, 0, this.Height);
  20.             this.IGraphicsPath.AddCurve(ICurve);
  21.             this.IGraphicsPath.AddLine(this.Width, this.Height, this.Width, 0);
  22.             this.IGraphicsPath.AddLine(this.Width, 0, 0, 0);
  23.  
  24.             this.IGraphicsPath.CloseFigure();
  25.         }
  26.  
  27.         internal System.Drawing.Point[] ICurve;
  28.         internal System.Drawing.Drawing2D.GraphicsPath IGraphicsPath;
  29.         public System.Drawing.Drawing2D.GraphicsPath GraphicsPath { get { return this.IGraphicsPath; } }
  30.  
  31.         internal System.Drawing.Size ISize;
  32.         public System.Drawing.Size Size { get { return this.ISize; } }
  33.         public int Width { get { return this.Size.Width; } }
  34.         public int Height { get { return this.Size.Height; } }
  35.     }
  36.  

HOW TO USE:

Expand|Select|Wrap|Line Numbers
  1.         public void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  2.         {
  3.             BottomCurve BC = new BottomCurve(this.Width, 100, 40);
  4.             e.Graphics.FillPath(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 255, 0, 0)), BC.GraphicsPath);
  5.         }
  6.  
Oct 24 '11 #1
1 1803
GaryTexmo
1,501 Expert 1GB
You get that red X when an unhandled exception occurs in a paint. It appears to be how the .NET framework handles it. It also looks like when an unhandled exception occurs, you can't really recover from it. I took your code and had the curve height set by a variable that I incremented to / decremented from with buttons and once it went into an unhandled exception it didn't come back, even if I reduced the curve height.

Anyway, this unhandled exception is entirely your doing :D On line 5 of your code above, you're throwing an exception when the curve height is greater than the height... which you intend, but what you're not doing is catching that exception anywhere. This is where the try/catch block comes in... you attempt something and if there's an exception, you handle it appropriately. I made the following changes to your paint method:

Expand|Select|Wrap|Line Numbers
  1. protected override void OnPaint(PaintEventArgs e)
  2. {
  3.     base.OnPaint(e);
  4.  
  5.     try
  6.     {
  7.         BottomCurve curve = new BottomCurve(this.Width, this.Height / 2, m_curveHeight);
  8.         e.Graphics.FillPath(new SolidBrush(Color.Yellow), curve.GraphicsPath);
  9.     }
  10.     catch (Exception ex)
  11.     {
  12.         e.Graphics.DrawString(ex.Message, this.Font, Brushes.Red, new PointF(0f, (float)this.Font.Height));
  13.     }
  14.  
  15.     e.Graphics.DrawString("Curve Height: " + m_curveHeight.ToString(), this.Font, Brushes.Black, new PointF(0f, 0f));
  16. }
Here I'm putting the curve instantiation inside a try/catch block. If there's an exception, I output an error message. If there's no exception, the try block executes completely and will draw the curve.

There is something you should be aware of though. There is an overhead associated with exception handling and while today's processors handle it fairly well, you may want to consider a more efficient approach. There are two places I can see where you can immediately increase your efficiency.

1) Instead of having a curve generated in the constructor so that you need to make a new curve every cycle, consider instantiating a single curve object and then updating it. There's overhead associated with creating, and subsequently destroying, an object. When you do this in a draw loop (assuming you have a draw loop) you're causing needless object creations which will only get released for garbage collection immediately. So instead of doing that work in the constructor, maybe move it to a GenerateCurve method which takes the same parameters. When called, it will update the class members and regenerate the curve.

2) Instead of an exception, consider a return value. If the curve generation sees anything it doesn't like, it can just return false. On the drawing side, if a return value of false is seen the appropriate action can be taken. This does take away from the error messages that can be seen though, but you can do other things like return a string instead of a boolean. If the string is empty, curve generation was successful. This might not be preferable to you though.

As I mentioned, today's processors can handle exception handling fairly well so the second one isn't that big a deal. Actually neither are, but they are something to consider if you're going to have a high drawing demand. If not, carry right on :)
Oct 24 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: arun gunda | last post by:
I want to throw exception dynamically. This what I want to do For example I want to throw System.Net.WebException exception. I will know the full exception name at run time, can I create a...
3
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo...
2
by: Dave | last post by:
Josuttis states that I may not throw an exception of type exception or of one of the standard exception types used for language support. Where in the Standard am I forbidden from "throw...
2
by: TS | last post by:
i'm wondering if it is preferred practice to throw exception in this circumstance. I have seen it done like that, but i have also read that you should try to never throw an exception in...
1
by: z. f. | last post by:
in vb asp.net page i'm overriding the finalize method in order to make cleanup. if i throw exception there it is not seen on the page. probably because the page has already sent to the client. is...
3
by: Ryan Liu | last post by:
Hi, In the .NET Framework SDK documentation, I can see DataRow.AcceptChanges method will throw RowNotInTableException exeception. And in DataTable.AcceptChanges(), the documentation does not...
5
by: Rob Dob | last post by:
I am trying to set the NullValue within the Column properties of my Dataset in VS2005. The DataType is a System.DateTime. and when I try and change it from "(Throw Exception)" I get the following...
0
by: Steve B. | last post by:
Hi, I'm wondering how to correctly throw exception within ASP.Net pages. I've page wich which waits for an "id" parameter in the querystring. I want to validate this param. I've wrote this...
1
by: =?Utf-8?B?TVIgRQ==?= | last post by:
This may seem like a stupid question but in C#: Say for instance I have a set of SQL processes that I run via ExecuteReader(). These processes return several pieces of information to the...
4
by: George2 | last post by:
Hello everyone, In Bjarne's book, it is mentioned that sort of STL may throw exception, like sorting elements in a vector. In what situation will sort throw exception? I can not find a case....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.