|
Requiring exceptions to be caught will eventually make people code with bad habits. Most of the time you dont want to catch the exception anyway, because you cant do anything about it at that level. I ask myself two questions about a block of code, when it comes to exceptions being thrown by it: 1. Can I recover from it? If yes, then catch and write recovery code. 2. Can I provide useful information about the context in which the exception was thrown? If yes then catch, and throw a new exception with the catched as inner exception.
Most code blocks will not fall under these categories, and should not have enforced catches. Forcing people to write catches will bloat the code so it's not readable (catches everywhere, the meaningful catches dont stand out). MS did a good design choice to leave out enforced catches.
BTW, to me exceptions are thrown mainly because of reasons out of your control: failures in the platform, execution engine, 3'd party library, etc. Or bugs in your code (arguments out of range, asserts gone bad, etc). They should never be used for general error handling.
Regards,
Björn Morén
Stockholm, Sweden
|
|
|
|
|
Actually I believe not enforcing exception handling is what make people code with bad habits. With
unchecked exceptions it's just too easy to ignore exception handling all together! You call a method on an object, you don't really know what exceptions it *might* throw, you don't have time to find out and you just forget exceptions even exists. The compiler won't complain, the code will run and everyone's happy.
MyObject myObject = new MyObject();
myObject.doSomething();
If I don't know what doSomething throws at me, in the best case scenario I could catch the generic Exception. But that still isn't good practice, right? Maybe I just leave it and let it propagate to my caller, who will be even more clueless about the exceptions that could generate inside doSomething.
But if you had doSomething throwing checked exceptions you wouldn't have a choice but to deal with them. And if you consider you can't do anything about it, Java will let you propagate it using the throws statement in your method declaration. The difference is it's all explicit and clearer. You know what's coming at you and you can decide to handle it or explicitly tell your caller to do it.
Cheers.
|
|
|
|
|
Sorry I forgot about Java's ability of exception propagation in the method declaration. That's an improvement for sure.
I agree that checked exceptions makes sense at the API level, but not inside the actual code library. It will be counter productive, due to reasong I wrote about earlier.
The ideal situation for me would be: Some way of getting information about possible exceptions being thrown by a method. And at the same time not forcing me to write checked exceptions Java-style.
Suggestion: the C# compiler inserts meta-information (method attributes) into the dll about what exceptions can be thrown by a method. It ought to be able to figure that out at compile time. As a code library user Visual studio can then inform me about those exceptions by use of intellisense or similar. When typing three slashes to generate my method documentation skeleton, VS could also automatically insert exception information and make that a part of the documentation (no need for the programmer to write anything).
Maybe something to suggest to the VS2005 team.
Regards,
Björn Morén
Stockholm, Sweden
|
|
|
|
|
my application uses buttons and listview item.And a textfield that I made.Its not a component , I made it with using drawing classes and it catches the keys that pressed then draws them to screen.Main problem is I need to use arrow keys to move the cursor that I made for my textfield.but listview or butons catch them before me ,items in the listview gets focus when I press the arrow keys if I disable the lisview then focus on the butons moves.After a few move my cursor starts to work becouse listview gains the focus for the last item in the row. .I need to make them disable only for the arrow keys.They gotto be enabled for mouse action....
any help?
|
|
|
|
|
Can anyone help me out with this and tell me what I'm doing wrong?
1. I have a form with a ListView control which will hold items for me...
2. I have declared a private variable of type ListViewItem ie.
private ListViewItem lvi = null;
3. In the ListView's MouseDown Event Handler, I check if the right mouse button is pressed, and to popup a context menu to delete the selected item.
private void ListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
try{
if (e.Button == MouseButtons.Right && this.ListView.Items.Count >= 1){
this.ListView.Focus();
this.lvi = this.ListView.GetItemAt(e.X, e.Y);
if (this.lvi != null) this.lvi.Selected = true;
this.cntxtMenu.Show(this.ListView, new System.Drawing.Point(e.X, e.Y);
} /* if (e.Button == .....) */
}/*end try */
catch{}
}
4. cntxtMenu contains a Caption 'Delete' and the menu-item is called cntxtMenuDelete.
5. In the cntxtMenuDelete Click Event I have the following
private void cntxtMenuDelete_Click(object sender, System.EventArgs e)
{
try{
this.ListView.Items.Remove(this.lvi); /* THIS LINE CRASHES! */
}/* end try */
catch{}
}
I have omitted the Exception code for brevity here, in the comment above where I've highlighted the line that causes a crash...the message I get from the Exception handler is, mind you, it doesn't even tell me what kind of exception is it - "Specified argument was out of the range of valid values. Parameter name '0' is not valid for 'displayIndex'." It even cut across the exception handler and therefore it did not catch the problem. I loaded this code into the CLR debugger and got it to trap all Exceptions and got a ArgumentOutOfRangeException - guess where, not in my code but in system.windows.dll....weird! the CLR debugger says it crashed when the Listview was making a call to get_item(int displayIndex) based on the stack dump from the debugger. ie a LVM_GETITEM message, something went funny there....I even derived the Listview and added a custom WndProc override to trap the LVM_GETITEM message and to ignore it....thinking that would solve it but no...Any ideas? Is this a bug?
Thanks in Advance,
Tom.
|
|
|
|
|
Hello Tom.
I hope write well!! because i'm from Argentina and a have 2 works to do, think the answer and translate it! .
I copy your code in a project that i use for test.
In the only case that happends, is when the listview remove the last lisviewitem, because for dafault, take focus the next listviewitem.
if after de "line crashes" write something like that
MessageBox.Show(" there are " + this.listView1.Items.Count.ToString());
the message take focus, and the error don't happends.
I dont know if this is a bug, I never have it because i ever confirm the delete.
I use a CommandButton near the listview (with a "-" icon)
and the code is
if (this.listView1.SelectedItems.Count >0)
{
if (MessageBox.Show (this, "¿Are you agree, for delete the item
selected?"," ",System.Windows.Forms.MessageBoxButtons.OKCancel)==
DialogResult.OK)
{
this.listView1.Items[this.listView1.FocusedItem.Index].Remove();
}
}
Bye
|
|
|
|
|
Many Thanks,
On Closer inspection after debugging, the call stack shows that with the code at hand, it somehow thinks it was initiating a drag and drop operation, Bizarre!! There's a few lines in the call trace that was calling WM_NOTIFY message, then calls LVN_BEGINDRAG, which in turn calls the ListView_getItem(displayIndex) and since the listitem was the only one, removing it, and boom, system.windows.forms.dll crash! I cured it by changing the event to a MouseUp Event which begs the following questions,
1) What is the real difference between MouseUp/MouseDown event handler apart from the fact that the button is released and the button is down respectively?
2) What situations would I use the MouseDown/MouseUp event handler and what would be a good reason to do so?
2) Why did it work with the MouseUp event handler? I used my hunch on this and changed the event handler, for some inexplicable reason, the listview control wasn't expecting a drag and drop and so it happily removed it!
Many thanks again!
Tom
|
|
|
|
|
1) What is the real difference between MouseUp/MouseDown event handler apart from the fact that the button is released and the button is down respectively?
Vanesa: if you click with the mouse and you do it on the wron place... using the MouseUp event you cant move the mouse (without relese the button) to another place and do nothing, while if you use the MouseDown, the event run when you click the mouse. This is the diference that I note.
2) What situations would I use the MouseDown/MouseUp event handler and what would be a good reason to do so?
Vanesa: I use MouseUp for example when I search address for a customer name, if datatable (returned for the sql query) contains more than 1 row, I send it to a especial form with a datagrid, and the user can select the customer to view his address,
private void dataGrid1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if(hti.Type == DataGrid.HitTestType.Cell)
{
dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
dataGrid1.Select(hti.Row);
// this is a control that "public" the selected id to the others forms
this.textBox1.Text = dataGrid1.CurrentRowIndex.ToString();
this.Close();
}
}
and the program return to the form that call the grid
Bye
Vanesa Addicted To DJ Tiësto's Trance
|
|
|
|
|
Many thanks for that insight! Certainly clarified everything!
|
|
|
|
|
Remove this line
this.cntxtMenu.Show(this.ListView, new System.Drawing.Point(e.X, e.Y);
attache your contect menu like below in your in your InitializeComponent() method
this.ListView.ContextMenu = this.cntxtMenu;
Sandeep Naik
|
|
|
|
|
Great thanks for your solution, even I changed the event to MouseUp it worked! But nonetheless I'll certainly remember this! ;)
|
|
|
|
|
Hi all, I'm again,
In my project I want to draw some points with "drawEllipse" dependent on a switch. Now my question:
Is it better to save the points in an Array like "int[] Points1 = new int[5] { 12, 14, 17, 45, 70 }; and then draw the points in a loop or should I draw the points in the switch one after the other with the drawEllipse?
What's with the performance and what's with the resources?
Thanks in advance
BB
|
|
|
|
|
Hi all,
I want to build a switch for the ". How can I do this?
Thanks in advance
BB
|
|
|
|
|
You can use characters like double quotes by incorporating the escape character \. See also: .NET Framework General Reference - Character Escapes[^]
If you're switching a string try this:
switch (MyString)
{
case "\"":
MessageBox.Show("It's a \"!");
break;
}
Or rather this example, if you're switching a char :
switch (MyChar)
{
case '\"':
MessageBox.Show("It's a \"!");
break;
}
Best regards
Dennis
|
|
|
|
|
Thanks Dennis, I see I was on the right way but I allway forgot something.
|
|
|
|
|
Hello
could anyone help me to find a way to embed a numericupdown column in a dataGrid??
thanks..
bhshs
|
|
|
|
|
Hello..
Could anyone help me to find a way to embed a numericupdown column in a datagrid??
thanks..
bhshs
|
|
|
|
|
hi,
will you please see documentation for
DataViewGrid.
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
I have two forms and form1 call form2 where some data are in arraylist with some struct. How can send the struct with data from Form2 back to 1.
Best Reagrds
Danyboy
|
|
|
|
|
|
I had implement event to do this but i still have problems. there is any example.
form_1
...
bt_Click(object sender, System.EventArgs e)
{
form2 myform = new form2();
form2.Notify+=new eventHandler(myform2_Notify);
form2.show();
}
myform2_Notify(object sender,EventArgs e)
{
// AS far i undertand here i put the code to //handle the data thats comes form form2 by //"sender"
// I want to receive form form2 the struc Reg
}
.....
form2
public event Eve EventHandler Notify;
protected void OnNotify()
{
if(NotifyDaddy !=null)
//Reg is public struct
NotifyDaddy(Reg,EventArgs.Empty);
}
private void bt2_Click(object sender, System.EventArgs e)
{
this.OnNotifyDaddy();
this.Close();
}
DanyBoy
|
|
|
|
|
Here is a quick example:
public class Form1 : System.Windows.Forms.Form
{
public delegate void AddDataEventHandler(string s);
public event AddDataEventHandler DataChanged;
public void OnDataChanged(string s)
{
if(DataChanged != null)
DataChanged(s);
}
private void someButton_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
this.DataChanged += new AddDataEventHandler(f2.AddData);
f2.Show();
}
private void AddData_Click(object sender, EventArgs e)
{
string val = this.textBox1.Text;
if(val != null)
this.OnDataChanged(val);
}
}
public class Form2 : System.Windows.Forms.Form
{
private ListBox listBox1;
public void AddData(string s)
{
this.listBox1.Items.Add(s);
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
Thanks for your help
I try to adapt for wath I want but only works with a string if a define on delegate
some class src.tools ...
public delegate void test1Event(main1.sub1 Reg);
form1
public event src.tools.teste1Event DataChanged
will not work. Reg.val1 we always =0
if i try to read by Reg.Tostring(); I got "main1.sub1+Reg"
best Regards
DanyBoy
|
|
|
|
|
sorry its working Thanks.....
DanyBoy
|
|
|
|
|
Hi,
I'm trying to implement a journal playback proc in C#...the journal record seems to be working fine...however the HC_SKIP in playback proc seems to never get reached...apparently windows keeps calling the playback proc with code HC_GETNEXT only...so i can't advance to the next message until HC_SKIP is called...has anyone ever had this issue?
Thanks
|
|
|
|