|
try this:
try
{
.....
.....
}
catch(Exception ex)
{
......
......
}
|
|
|
|
|
edaindia wrote: Is there a way to get a list of all possible exceptions that can be thrown by the application and the line number where it can be thrown
Not that I know of. Well, that's a bit of a lie; you could loop through all the assemblies that are loaded, and list those classes that inherit from Exception.
The programmers should already be handling the errors that they can expect locally with a try-catch block, as has been noted a few times. The exceptions that remain should be handled in the "global exception handler". There's an event called ThreadException [^] that gets raised whenever there's some exception that didn't get handled by the try-catch blocks.
If you don't handle those, you'll get an ugly exception-screen. Now, you want a list of all the exceptions that can occur there? Well, everything that's derived from Exception - hence the suggestion to just log "everything" that triggers the ThreadException event.
Good luck
I are Troll
|
|
|
|
|
Eddy Vluggen wrote:
Not that I know of. Well, that's a bit of a lie; you could loop through all the assemblies that are loaded, and list those classes that inherit from Exception.
Is there any way to do this?
Eddy Vluggen wrote: The programmers should already be handling the errors that they can expect locally with a try-catch block,
My concern was about finding those sections of code which should be within a try catch block, but are not.
If the exception was caught at the right location the programmer would have various options (e.g. abort,retry,ignore). Hence the emphasis on finding a formal way to identifying all code sections which can throw an exception and resolving them through a code review.
A global exception handler would be my last resort at the time of actual deployment.
Thanks for the reply....
|
|
|
|
|
edaindia wrote: Is there any way to do this?
Yes. Loop through all the assemblies in your domain, enumerate all classes within that assembly and check what they are made (derived) of.
Will that get you a list of all exceptions that can be thrown? Well, yes and no: it will give you a list of known exception-classes. Some classes might be generated at runtime, some are wrapped in global classes and only carry a cryptic number from the filesystem. Another point would be that you'd have some exceptions on that list that would be very rare. In other words, you'd end up with misleading information - preparing for StackOverflow-exceptions that simply never occur.
edaindia wrote: My concern was about finding those sections of code which should be within a try catch block, but are not.
With all respect, that's the programmers' own responsibility. You don't want an "on error resume next" system, and you don't want to force a user into deciding "ignore", "retry" and "cancel" for a StackOverflow. They'll get confused. I find myself often clicking the X-button in such a situation.
edaindia wrote: A global exception handler would be my last resort at the time of actual deployment.
Make it the first resort, and log everything that happens. Followup on everything, untill they slowly die out.
You can't avoid falling down, there will always be an exception that you didn't cater for. Or worse, a method that fails without throwing an exception. You don't want a customer relying on a backup that failed silently!
Once that you know that there will be an unhandled exception, your best option is to divine a strategy to handle them. Ie, implement that global exception-handler, and follow up on those rare exceptions that could actually harm your clients.
Think about it; what you're suggesting is closed to the default behaviour of a .NET application. It makes sense to shut-down the application, since there's an exception. And there's no way that you're going to surround each and every method with a OutOfMemoryException - some things can be translated to a simple message from a global point.
edaindia wrote: Hence the emphasis on finding a formal way to identifying all code sections which can throw an exception and resolving them through a code review.
Sounds rather hostile. I suppose that you could make a tool that assists the programmer in identifying methods that throw known exceptions. You'll get a hint which exceptions might be thrown when you hover your mouse over a method. Ergo, it should be possible to highlight those lines with a plugin for Visual Studio.
In theory that is
I are Troll
|
|
|
|
|
edaindia wrote: a list of all possible exceptions
No.
FYI: The list of things that can go wrong in Windows would be infinite.
|
|
|
|
|
And more often than not, there's not a damn thing you can do about it.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Isn't that called StackOverflow?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Just put a try/catch block around everything that could fail, and there you have it.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
|
I have done plenty of googling and have gotten close to a solution but wanted to make sure I'm going about it the right way.
I have a data file which is a non delimited fixed field position file that has missing data. I basically want to read each line in and determine if a certain field is blank. If so I use another field in that same record to retrieve the missing data from the database. After getting the missing value I would like to use StreamWriter or StringWriter (not sure which) to write out each line to a new file with the updated field value.
Appreciate any advice you all may have on this subject.
Thanks,
Jason
|
|
|
|
|
I would advice you to do the following:
1. Open the input file
2. Open the output file and create StreamWriter for it
3. Read data line-by-line, and for each line do the check and write to the output file
You need StreamWriter.
The difference:
StreamWriter allows you to write contents to any stream: file, socket, etc;
Stream represents writeable file, socket or other stream (this can be also a memory, or string) - it's not convenient to write strings to stream, that's why you need StreamWriter.
StringWriter writes all contents to a string (in memory);
StringBuilder allows you quickly build string from parts; you needn't it here
|
|
|
|
|
Thanks for the steps and showing the difference between the different classes. Below is what I have so far and I think it will work. My new file contained the 111222333 string for all the missing data which was the desired result.
Thanks,
Jason
StreamReader sr = File.OpenText("C:\\Source.txt");
StreamWriter sw = File.CreateText("C:\\Test1.txt");
while (sr.Peek() != -1)
{
string strRecord = sr.ReadLine();
string strSurname = strRecord.Substring(0, 10);
string strFirstName = strRecord.Substring(11, 8);
string strPupilNum = strRecord.Substring(20, 9).Trim();
string strSSID = strRecord.Substring(373, 9).Trim();
if (strPupilNum == String.Empty)
{
// call db stored procedure to retrieve PupilNum based on SSID
StringBuilder sb = new StringBuilder(strRecord);
sb.Remove(20, 9);
sb.Insert(20, "111222333", 1);
strRecord = sb.ToString();
}
sw.WriteLine(strRecord);
} sw.Close();
|
|
|
|
|
Yeah, that's it.
Don't forget to dispose your StreamWriter and StreamReader .
And next time please wrap your code with <pre lang="C#"> ... </pre> tags
|
|
|
|
|
I am currently working on a simple game for the children in C# WPF. It uses the simple animation from Expression Blend. All that is done is a simple textblock falls in the apps and I wish to make a loop of it such that everytime the loops restarts that is the textblock falls the new contents are displayed to it. XML and LINQ to XML is what I plan to use.
The real problem is the storyboard codes are in XAML and it needs to be looped and added new features from the C# codes. I seem to have no idea how to accomplish that.
I would be grateful if anyone could help. Any favors will be appreciated.Than you in advance.
|
|
|
|
|
anishshrestha wrote: The real problem is the storyboard codes are in XAML and it needs to be looped and added new features from the C# codes
So what's the problem?
Storyboard myStoryBoard = (Storyboard)FindResource("myStoryBoard");
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
hi,
i need to show selected country location through Google map with windows application, so i used "GMap.NET.dll" ,prior it was worked fine, but it is not working , could you please suggest me why it is not working ,and give me suggestion for better way to do this.
|
|
|
|
|
GMap doesn't work in India.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
what do you mean it doesn't work in India?
peace & serenity
|
|
|
|
|
It's called "humor".
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
peace & serenity
|
|
|
|
|
Hi There
I am using Aspose.pdf to generate pdf documents.
The pdf document contains a table which is populated with data from a database.
I have the problem that if the table needs to continue to the next page, the pdf does not generate.
Is there any code that I need to include to allow the document to continue to a new page?
Regards,
|
|
|
|
|
hi,
you can use itextSharp.dll to generate pdfs ,this will get from sourceforge.net , and reference to your project , then write code like this
Document dc = new Document(PageSize.LETTER);
dc.NewPage();
|
|
|
|
|
Thanks, I give it a try
|
|
|
|
|
Just my 2c, I found itextSharp very easy to implement.
|
|
|
|
|
Hello everybody,
I have a file where some application is storing some values. I want to access to this file in read mode but when I try to open the file I have always the message that the process can't acces the file because it is being used by other application.
If I try to open this file with Microsoft excel I have an alert that says that the file is user by other application but I am allowed to open the file in read mode only.
How can I access the file with .net and advoid the exception if it is possible?
Thanks in advance
|
|
|
|