|
Hello!
Ive got a Method1 with a try-catch. In Method1 I call Method2 which also has a try-catch. When exception is thrown in Method2, and the catch sequence is run, I want the application to end the current operation(not end the whole application, just what the user tried doing last). Instead the application keeps going in Method1 (where another exception soon will be thrown since Method2 wasn't run correctly). What is the best way of doing this?
/regards
|
|
|
|
|
You could rethrow the exception catched in Method2 or throw a new exception (preferrably using the catched one as inner exception), so Method1 is noticed something has gone wrong in Method2 and its try-catch gets immediatly entered.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Use the finally clause after the catch
|
|
|
|
|
|
welcome
|
|
|
|
|
i would return a success state from method2. If the exception that you caught in method2 could reasonably be described as expected (file does not exist, server not available etc) then i would not rethrow the error. If , however, the error is unexpected I would rethrow the Error adding the original error as the inner exception of the new exception object.
Let's say string method2 () is a load file method
If the user cancels the dialog or the file is not a text file and can't be read then instead of returning a string containing the text from the file i return a null. Method1 sees a null and exits without carrying out further work. If while trying to open the file an unrecoverable error occurs let's say a "Your Harddisk has borked" Error then I create and throw an exception and let the main app deal with it.
I'm a great believer in only throwing errors as a last resort and trying to not use them in the expected flow of an application.
Russell
|
|
|
|
|
If you catch the exception in Method2 and pretend that nothing is wrong, Method1 will be unaware of the error condition.
Re-throw the exception or throw a new exception in Method2, and handle that in the catch in Method1.
Alternatively (but this is sometimes frowned upon in object orientation) you can return a status from Method2 that tells Method1 if it succeeded or not.
---
Year happy = new Year(2007);
|
|
|
|
|
Hi All,
I am using a c#.NET windows application,
is there any explicit method for comparing two folders according to their size and date modified?
Send Me any Related Link.Please it's Urgent
thanks
Tirumal
|
|
|
|
|
AFAIK the only way is to compare folder contents, a file at a time.
Luc Pattyn
|
|
|
|
|
tirumal1231 wrote: .Please it's Urgent
It seems that it always is.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
To compare the date modified you can simply use DirectoryInfo.LastWriteTime . To compare the size I am afraid you have to recursively go through directories and their subdirectories and sum up FileInfo.Length of contained files.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
There is now Explicit Method but try this.
There may are some mistakes because i wrote it without much testing.
private long FolderSize = 0;<br />
<br />
private bool comparefolders(string path1, string path2)<br />
{<br />
string strdate_path1 = string.Empty;<br />
string strdate_path2 = string.Empty;<br />
long foldersize1 = 0;<br />
long foldersize2=0;<br />
bool folders_compare = false;<br />
try<br />
{<br />
DirectoryInfo dirinfo_path1 = new DirectoryInfo(path1);<br />
DirectoryInfo dirinfo_path2 = new DirectoryInfo(path2);<br />
if (dirinfo_path1.Exists == true && dirinfo_path2.Exists == true)<br />
{<br />
strdate_path1 = dirinfo_path1.CreationTime.ToString();<br />
strdate_path2 = dirinfo_path2.CreationTime.ToString();<br />
foldersize1 = GetFolderSize(path1);<br />
foldersize2 = GetFolderSize(path2);<br />
if (foldersize1 == foldersize2 && strdate_path1 == strdate_path2)<br />
{<br />
folders_compare = true;<br />
}<br />
}<br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
string error = ex.Message;<br />
}<br />
return folders_compare;<br />
}<br />
<br />
private long GetFolderSize(string Root)<br />
{<br />
FolderSize = 0;<br />
SeekFiles(Root);<br />
return FolderSize;<br />
} <br />
<br />
private void SeekFiles(string Root)<br />
{<br />
string[] Files = System.IO.Directory.GetFiles(Root);<br />
string[] Folders = System.IO.Directory.GetDirectories(Root); <br />
FileInfo File;<br />
for(int i=0;i<Files.Length-1;i++)<br />
{<br />
File = new FileInfo(Files[i]);<br />
FolderSize += File.Length;<br />
}<br />
<br />
for(int i=0; i< Folders.Length-1; i++)<br />
{<br />
SeekFiles(Folders[i]);<br />
} <br />
}
-- modified at 4:26 Friday 19th January, 2007
|
|
|
|
|
sir this is aravind here ,,i cant able to run the program,,if u can able to send full program(A-Z),,i am new to Dotnet,,,,,plz
|
|
|
|
|
Well, there are some additional considerations here:
- if you add a new file in one of the subdirectories, it does not change the last write time
of the top directory
- if you delete a file and create another with equal size, it does not alter total size.
So be careful about when you are satisfied nothing happened to a directory !
BTW: I am not sure about this, but the exact behavior could also depend on the underlying
file system (FAT, FAT32, NTFS, ...)
Luc Pattyn
|
|
|
|
|
Thats right but he only wanted an method two compare the size and the modify date of the folder and not of the files in it.
|
|
|
|
|
hi
i'm trying to set the location of a panel through my code.
how can i do this. is it done through the location property.
if yes then how do i manipulate this property??
regards
Saira
|
|
|
|
|
Example:
panel.Location = new Point(5, 5);
|
|
|
|
|
thank you
|
|
|
|
|
I have 2 forms:
1- Main form viewing a datagrid of different values of database X
2- A secondary form used for adding new data into database X
When any data is entered in Form2 and Form2 is closed; it's needed to update datagrid with new values in Form1. But it doesn't update quickly, a 3-4 second late. Using Access database, I've found a nasty dirty solution : Having a timer and updating datagrid periodically. Is this a problem about Access slow query time or any problem about forms or etc? Any idea ???
|
|
|
|
|
Hi, I'm working on an open source IDE that has a built-in syntax highlighter.
Right now, whenever I want to remove text from the RichTextBox, I have to do this:
<br />
RTB.Text = RTB.Text.Remove(CharacterIndex, RemoveAmount);<br />
The problem is that changing RTB.Text erases all the Rich Text Format information generated by the syntax highlighter.
So my question is, how do I remove text from the textbox without resetting the RTF data?
|
|
|
|
|
It does work!
Just pay attention to any ArgumentOutOfRangeException that is thrown when either your CharacterIndex or your RemoveAmount is less than zero.
SkyWalker
|
|
|
|
|
Hi,
I'm a intermediate java programmer and i just started learning C#.I would like to write an application that takes a txt file and send it to a fax machine. What is the best way I can do this? Thanks in advance ,any help would be gladly appreciated
|
|
|
|
|
wrote: I'm a intermediate java programmer and i just started learning C#
As a former Java developer myself, welcome to the wonderful world of C#.
Regarding your question, have you looked at any of the articles on this site? This article[^], for example, talks about how to fax an image. It shouldn't be too hard to figure out from there.
Also note that all file-related APIs are nice and organized in System.IO namespace (a "namespace" is like Java's "package"). For example, to read all text from a file, you can simply go System.IO.File.ReadAllText("theFile.txt");
|
|
|
|
|
thanks for the quick reply Judah, I will take a look into that article.
|
|
|
|
|
I get the following error when I try to send the fax.
com object with clsid{d73733c8-cc80-11d0-b225-00c04fb6c2f5} is either not valid or not registered
How do I reference to the faxcom lib?
|
|
|
|