hello
we have this code :
Code Snippet
public class GrowLabel : Label { private bool mGrowing; public GrowLabel() { this.AutoSize = false; } private void resizeLabel() { if (mGrowing) return; try { mGrowing = true; Size sz = new Size(this.Width, Int32.MaxValue); sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak); this.Height = sz.Height; } finally { mGrowing = false; } } protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); resizeLabel(); } protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); resizeLabel(); } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); resizeLabel(); } }
I begins then I went on msdn to see these functions:
Size sz = new Size(mMaxWidth, Int32.MaxValue); http://msdn.microsoft.com/en-us/library/system.windows.size.aspx
sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak); http://msdn.microsoft.com/en-us/library/8wafk2kt.aspx
this.Height = sz.Height; http://msdn.microsoft.com/en-us/library/system.windows.forms.control.height.aspx
and in these URLs, these functions have no exceptions
I would like to know the reason to put these lines in a try? please
|
| Co2r Wednesday, November 12, 2008 3:09 AM |
| Co2r wrote: |
|
I would like to know the reason to put these lines in a try? please
| | I don't know. Why do you put them in a try? You shouldn't.Where do you have that code from? |
| Lucian Baciu Wednesday, November 12, 2008 4:25 PM |
|
| Co2r Wednesday, November 12, 2008 4:42 PM |
|
| Lucian Baciu Wednesday, November 12, 2008 4:53 PM |
|
| Co2r Wednesday, November 12, 2008 5:03 PM |
Hi Co2r
The finally block is useful for cleaning up any resources allocated in the try block. Control is always passed to the finally block regardless of how the try block exits. So this try-finally block is not for catching the exception.
Sincerely,
Kira Qian |
| Kira Qian Thursday, November 13, 2008 6:08 AM |
I'm ok for the bloc "finally" but why put these 4 lines :
mGrowing = true; Size sz = new Size(this.Width, Int32.MaxValue); sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak); this.Height = sz.Height;
in a block "try" ? |
| Co2r Thursday, November 13, 2008 6:25 AM |
No catch, how come the finally? I guess the solution given by Nobugz might be use finally to clean the resource.
Sincerely,
Kira Qian |
| Kira Qian Thursday, November 13, 2008 6:42 AM |
yes, but write the code like this is the same no ?
private void resizeLabel() { if (mGrowing) return;
mGrowing = true;
Size sz = new Size(this.Width, Int32.MaxValue); sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak); this.Height = sz.Height;
mGrowing = false; }
Nobugz told me that there are a possible throw and I would like to know which |
| Co2r Thursday, November 13, 2008 10:50 PM |
| Co2r wrote: |
|
yes, but write the code like this is the same no ?
private void resizeLabel() { if (mGrowing) return;
mGrowing = true;
Size sz = new Size(this.Width, Int32.MaxValue); sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak); this.Height = sz.Height;
mGrowing = false; }
Nobugz told me that there are a possible throw and I would like to know which
| |
Nobugz, that guy's wise. |
| Rudedog2 Friday, November 14, 2008 12:49 AM |
yes but i want to know his reason to put these lines in a try blok ! read a doc etc, i'm begins, i just want a link where i can read these line throw an exception just for to learn... |
| Co2r Sunday, November 16, 2008 4:18 AM |
Hi Co2r
Post your question on a thread with nobugz’s follow. Let him answer your question when he see the follow up notification information.
Sincerely, Kira Qian |
| Kira Qian Monday, November 17, 2008 3:12 AM |
nobody can tell ? |
| Co2r Wednesday, January 28, 2009 8:45 PM |
I already gave you an answer in your other thread, I'll repeat it again here. Your assumption that .NET methods only ever throw the exceptions that are documented in the MSDN article is incorrect. TextRender.MesureText() is a complicated method and executes lots of unmanaged code, it has many failure modes. Obvious ones are a null argument value. Non-obvious ones are nastiness like running out of handles, internal overflows due to very large strings or really big fonts, malfunction of the .NET P/Invoke machinery, heap corruption, running out of memory, a bad font resource. And everything else you didn't think of because you can't predict everything that can go wrong. None of which are documented.
Whatever mishap you'd encounter, without the finally block the control is dead as a doornail when MeasureText() throws an exception. That will leave the mGrowing member set to True and it is never ever going to be set to False ever again. That is just fine when you let the exception crash your program. Trouble is, it doesn't. You'll have to write code to actually make it crash. Code that the vast majority of WF programmers never write. Did you?
Hans Passant. |
| nobugz Thursday, January 29, 2009 3:11 AM |