|
Well I've discovered I still dont' know where to put a global try...catch block in vb.net. I figured it would go in the Mainform.mybaseload method but that doesn't seem to do it.
Thanks.
|
|
|
|
|
Hi again,
this is how it is done in C#:
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try {
Application.Run(new myMainForm());
} catch(Exception exc) {
log(exc.ToString());
}
}
}
The above code instantiates and executes the main form class.
For a regular Windows app, VB.NET seems not to rely on a Main() method, but directly
launches the startup object.
However I trust in a VB.NET module you could do exactly what the C# code does.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Can I disable my try..catch block handling while debugging? Thanks
|
|
|
|
|
Nope. It's part of your code. You'll have to comment it out before you start the debugger.
|
|
|
|
|
Too bad..that's why making too many of them makes it difficult to debug.
|
|
|
|
|
Not really. It's how you break down your problems and what you put in the Try/Catch block that really matters. (Hint, hint)
Also, it helps if you're not putting the bulk of your method code in the Try block. Keep the Try/Catch blocks small, encompasing only very small operations. Say you have a method that queries a database, then loops through the returned recordset and sends an email to every address it finds. There's a ton of places this can fall on its face. Do you put all of this in a single Try/Catch block?? Nope.
Really, this can be broken down into a bunch more methods, each handling very small parts of the process. Creating and returning a connection to the database is one, exectuing an arbitrary query is another, iterating over a dataset, pulling email addresses out, sending an email to an address, ... These are all seperate methods, each with it's own Try/Catch opportunities. The more atomic the operation of each method, the better.
Luc just nailed it in his post here[^].
modified on Friday, February 15, 2008 1:27 PM
|
|
|
|
|
In my experience, it is not the number of the try-catches that poses a problem;
a lot of people seem to stuff the catch blocks with MessageBox.Show which is a
terrible idea, since now you get tired of reading messages, one at a time, and having
to close them over and over again.
I prefer to provide a simple "log" method that takes a single string input, and
logs it somehow. I then implement the log based on Console.WriteLine, a file operation,
a listbox operation, or whatever suits my needs at the time. The advantages are:
- I don't have to interact with this logging
- I get it all sequentially (I tend to automatically add a timestamp too)
- I can save it, and compare different runs
I often temporarily add a ListBox to my main form, just for showing the logging.
MessageBox really is not my prefered debugging tool!
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
What I'm mostly having trouble with is determining where the error is, not the error handling itself. When my method is big, I can't tell where the error occurs within it, when it's running from the .exe on a different machine.
I don't seem to be getting any line numbers to help out. Perhaps I'm not in debug mode, although the .exe is written to the debug directory.
Thanks again.
|
|
|
|
|
strange, I just did a little VB app (VS2005 VB.NET Express) with a deliberate error,
and it exceptioned with the correct line number right away.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
cstrader232 wrote: not the error handling itself. When my method is big, I can't tell where the error occurs within it
This is a hint that your method is too big and needs to be broken down into smaller pieces. Also, it sounds like you're putting way too much inside the Try block. A Try block should encompass as few lines as possible that can fail. You don't wrap an entire method. You wrap small sections where each section has a specific task.
|
|
|
|
|
Hi Dave,
I beg to differ on this one, I would put as much code in a try block as logic dictates.
If my method needs 100 simple steps without any side-effects, and I don't care where
it fails, I'll put them all in a single try.
The net result would be either success, or failure in which case the details
of the failure are (should be) apparent from the exception, and probably the catch
block would contain code to throw an application-specific new exception (and store the
original as an inner exception).
If on the other hand, the steps have lots of side-effects (allocating resources,
opening files, etc) I would use more try/catch/finally blocks, even nested, to
handle this elegantly.
But I would avoid the fully sequential try one step, catch, try next step, ... approach
since then there could well be more error handling code than straight code, yielding lower
programmer efficiency, more code and more execution paths to test.
I do stress the use of line numbers while debugging though, a simple technique a lot
of people seem unfamiliar with (the fact that the Visual Editor by default does not
show them looks like a mistake to me).
Regards,
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Luc Pattyn wrote: I beg to differ on this one, I would put as much code in a try block as logic dictates.
If my method needs 100 simple steps without any side-effects, and I don't care where
it fails, I'll put them all in a single try.
True. I guess I tailored my answer to his case. If he can't find where the offending code is, that's a sign he's got too much in the Try block.
I try to keepo me Try's as small as possible, but it, agreeably, depends on the situation.
Luc Pattyn wrote: But I would avoid the fully sequential try one step, catch, try next step, ... approach
since then there could well be more error handling code than straight code, yielding lower
programmer efficiency, more code and more execution paths to test.
I think I should clarify that I'm not saying he should break each tiny step into it's own Try/Catch. I am saying that it should be closer to wrapping each step that makes up a transaction.
For instance, depending on the situation and granularity of the error handling in the requirements, wrapping creating and opening a connection object and running a query and returning a result set is probably going to be more "friendly" than wrapping each step in that same code. If you're going after a generic DAL library that executes arbitrary SQL on a database, then you might want to wrap each of those steps. It just depends on the flexibility or importance of the transaction in question.
Some transactions are tolerant of a single item failure and continue on. Others need to know so they can stop at a certain point. And still others need to backout in the event of a certain set of failures, but can continue on in the event of a smaller failure.
I've probably done a piss-poor job of explaining this. I apologize in advance. It's late in the day and I'm starting to fall asl.....
|
|
|
|
|
Dave Kreskowiak wrote: I've probably done a piss-poor job of explaining this
If so, you have amply rectified it.
Dave Kreskowiak wrote: It's late in the day
That's all relative.
Gnite.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Very good points there, Luc.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Does an exception give any information more specific than the method in which it occurs and the exception type and message? If you have a long method it is sometimes hard to see where the error is.
Thanks!
|
|
|
|
|
Hi,
an Exception holds a lot of information:
- the Message (a one-line string)
- extra information (such as the file path on a file I/O error)
- the Stack TraceBack containing a list of methods, with line numbers when available
- and the same for any inner exception.
To get it all you need to use ToString(). Just looking at Message won't help much.
To locate the problem line, use a debug build and look at the very first line number
that appears in the stack traceback.
BTW: you can teach Visual to always show line numbers in the editor, see Tools/Options/
TextEditor/...
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Thanks Luc!!... might I ask another question of you?... what's the best way to capture the exception within the entire project? I have specific try..catches, but I don't want to make a separate one for each method.
|
|
|
|
|
I'm not seeing line numbers in the stacktrace. How can I be sure I'm compiling in debug mode?
Thanks
|
|
|
|
|
cstrader232 wrote: How can I be sure I'm compiling in debug mode?
Not sure for VB.NET, I do most stuff in C#, where the Build menu offers a Configuration
item, letting you choose between Debug and Release.
I expect the Debug menu is visible only while building for and running with debug.
I recall having had some trouble with C++ and/or VB.NET to get it the way I wanted,
but currently my VB.NET (VS2005 Express) is running debug, and showing line numbers
in exceptions (only on user code, the .NET libraries don't show line numbers here).
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Hi,
proper exception handling is a rather complex matter. I'll try and give some of the
basics here:
1.
you should not catch an exception and then ignore it, since that would destroy useful
information, needed to remedy an exceptional situation; every exception should either
be solved (e.g. an alternative way gets attempted), or the problem should be
signalled to the caller (through a "result" return value, a new exception, or by
not catching it in the first place).
2.
normally you should catch an exception where you are able to deal with it,
and that often is pretty close to where it happens.
Example: if you have a mathematical algorithm and suddenly get a divide-by-zero,
it does not make sense to have that catched several method levels above it, since
at that level the caller would not be aware of the algorithm, what a zero-divide
would be, and how it should be remedied. Instead, you should catch it locally, and
try to organize things such that the method fails gracefully.
3.
it is often wise to include a specification on exceptions when specifying a
method or a library; exception handling is a basic part of the contract.
(Java enforces this, .NET does not; there are pros and cons tho).
4.
It does not hurt to have a top-level try-catch (that's in the static main method itself);
it should NEVER fire in production code, it's main purpose is to help the developer notice
all the exceptions that are lacking a local try-catch.
If it fires in production code, it halts the app, which never looks good; but it should
show all available info (hence ToString), and urge the user to send that to the developer.
5.
not all exceptions are easily noticed and dealth with; there are special measures to
deal with exceptions in constructors, in background threads, in remote objects,
in native code, etc.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Thanks Luc, Thanks Dave!
That's very helpful. I'm at the point where I need to move my app from few tryblocks to a whole lot of them. I can see that that is going to be good in the end.
Honestly, I'm not sure where the "static main method" is:
Luc Pattyn wrote: It does not hurt to have a top-level try-catch (that's in the static main method itself);
Happy weekend!
chuck
|
|
|
|
|
Sorry, "static main" is a C# notion. You may have a "sub main" in a VB module,
or you just have a main form somewhere. I'm not that familiar with VB...
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
OK, that's all making sense and I found some line numbers! So thanks again!
|
|
|
|
|
Luc Pattyn wrote: Example: if you have a mathematical algorithm and suddenly get a divide-by-zero,
it does not make sense to have that catched several method levels above it, since
at that level the caller would not be aware of the algorithm, what a zero-divide
would be, and how it should be remedied. Instead, you should catch it locally, and
try to organize things such that the method fails gracefully.
If I'm trying to do something like plot a graph of a user-specified function, what would be the preferred try/catch methodology for the expression evaluation? I know that one isn't supposed to use try/catch to just ignore errors, but if I get a divide-by-zero, domain error, or other such problem do I really care why the evaluation failed? I should regard the function value at that point as undefined (meaning that the points before and after should probably not be connected) but other than that I don't see much that a try/catch block could usefully do.
Ideally, one could divide exceptions from such a routine into three classes:
- Those whose failure should simply cause a particular value not to be plotted.
- Those whose failure should cause the remainder of the plotting to be skipped, since remaining points won't work any better.
- Those which should send an exception to outer functions, since something very bad is happening (heap management failure, etc.)
From a practical standpoint, though, I don't see any clean way to accomplish that if the function to be plotted will be supplied by the user. Unless the called function has defined explicit exception types for 'failure that's going to occur on all points' versus 'failure that may just occur on one point', I see no way for the outer function to know what to make of any inner exceptions.
|
|
|
|
|
Hi,
this is how I see it:
your plot class should define a number of exceptions for the cases 1 and 2 you listed;
that's the conditions that have a functional significance to the plot itself.
these exceptions become part of the contract between the user-defined function and the plot class.
your plot method should react appropriately to these exceptions; and it should also protect
itself against spurious exceptions, i.e. anything else the function might (but really
shouldn't) throw; these include your third group. Probably the protection would just entail
catching everything, and encapsulating it into a single PlotException, of course with the original exception as an inner one.
on the other hand the user-supplied function should contain its own exception handling,
it should catch things such as overflow, zero divide, etc. and either return whatever value
it considers appropriate, or (better) throw one of the exceptions your plot method has defined.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|