Stopping error message in VB .NET

  • Thread starter Thread starter Toni
  • Start date Start date
T

Toni

Hello

I'm a newbie in VB .NET and I'd like to accomplish a (I think) very
simple task.

What I want to do is throw an error message to the user whenever
something happens. The main point is that the message must stop the
code execution, but it sholud not close the application nor show the
classical exception window (the one with JIT compiler information).

That is, I'd like to make something like


Dim L_XMLConfigurationFile As XmlDocument = New XmlDocument

Try
L_XMLConfigurationFile.Load("c:\temp\TestFile.xml")
Catch ex As Exception
MyErrorFunction("The file <TestFile.xml> does not
exists.")
End Try

MessageBox.Show("We got there...")


and I want to get an error window telling the user that the file does
not exists, but I must not allow to continue the program execution.
That is, the program should never get to the MessageBox.Show("We got
there...") line if the file does not exists.

The exception handler mechanism shows a window that allows the user to
continue execution, so it does not fit my needs.

Some languages have an ERROR function that stops the execution where
it is called, and I want just that.

Any ideas?


Thanks,

Toni
 
Toni said:
Hello

I'm a newbie in VB .NET and I'd like to accomplish a (I think) very
simple task.

What I want to do is throw an error message to the user whenever
something happens. The main point is that the message must stop the
code execution, but it sholud not close the application nor show the
classical exception window (the one with JIT compiler information).

That is, I'd like to make something like


Dim L_XMLConfigurationFile As XmlDocument = New XmlDocument

Try
L_XMLConfigurationFile.Load("c:\temp\TestFile.xml")
Catch ex As Exception
MyErrorFunction("The file <TestFile.xml> does not
exists.")
End Try

MessageBox.Show("We got there...")


and I want to get an error window telling the user that the file does
not exists, but I must not allow to continue the program execution.
That is, the program should never get to the MessageBox.Show("We got
there...") line if the file does not exists.

The exception handler mechanism shows a window that allows the user to
continue execution, so it does not fit my needs.

Some languages have an ERROR function that stops the execution where
it is called, and I want just that.

Any ideas?


Thanks,

Toni

As the last statement in your MyErrorFunction(), which you are calling if
the attempt to load the config file fails, try this:

Application.Exit()

Going back to your question, you appear to make contradictory statements:
"The main point is that the message must stop the code execution, but it
should not close the application..."

and

"...and I want to get an error window telling the user that the file does
not exists, but I must not allow to continue the program execution."

If you do wish to "not allow to continue program execution", then
Application.Exit() is for you.
 
You could put the code in the try block. It won't get executed if the catch
is invoked before it is hit.

You could also just exit the Sub or Function using: Exit Sub or Exit
Function.

When you say you want the program to stop I'm guessing you mean you want
that particular funtion to stop, but that the user may choose to try
something else...?

Otherwise, use the Application.Exit as noted in a previous post.

Jerry
 
Jerry Camel ha escrito:
You could put the code in the try block. It won't get executed if the catch
is invoked before it is hit.

You could also just exit the Sub or Function using: Exit Sub or Exit
Function.

When you say you want the program to stop I'm guessing you mean you want
that particular funtion to stop, but that the user may choose to try
something else...?

Otherwise, use the Application.Exit as noted in a previous post.

Jerry


Hello

When you say you want the program to stop I'm guessing you mean you want
that particular funtion to stop, but that the user may choose to try
something else...?

Yes, I want the user may choose to try something else (maybe in the
same form where the error happened), so I don't want to close the
application.

I can't use the Exit Function or Exit Sub way because that wolud exit
the current function, but the main execution thread will continue from
there. And I want the main program execution to stop.

I'll try the Application.Exit route. Thanks!


Toni
 
(e-mail address removed) ha escrito:
Jerry Camel ha escrito:

Yes, I want the user may choose to try something else (maybe in the
same form where the error happened), so I don't want to close the
application.

I can't use the Exit Function or Exit Sub way because that wolud exit
the current function, but the main execution thread will continue from
there. And I want the main program execution to stop.

I'll try the Application.Exit route. Thanks!


Toni


Hi

Well, the Application.Exit line effectively closes my application, so
it is not for me. I want the user to remain into the program window
where the error arose.

The nearest thing I reached is simply don't doing nothing. That is, I
simply write the code without using the try/catch thing. If there is
an error the system throws an undhandeld exception (the dialog box with
a red cross in it) with some information about the error. The user can
hit the Continue button and the program won't close; the user remains
where he was before the error.

I'd like to have control on the text that the dialog shows with the
exception, and make the code execution stop there in order to let the
user try something different.


Toni
 
Well, the Application.Exit line effectively closes my application, so
it is not for me. I want the user to remain into the program window
where the error arose.

The nearest thing I reached is simply don't doing nothing. That is, I
simply write the code without using the try/catch thing. If there is
an error the system throws an undhandeld exception (the dialog box with
a red cross in it) with some information about the error. The user can
hit the Continue button and the program won't close; the user remains
where he was before the error.

I'd like to have control on the text that the dialog shows with the
exception, and make the code execution stop there in order to let the
user try something different.


Toni
Doing nothing about exceptions is not good design, and if you leave it at
that, your program will likely just crash when run outside the IDE. The
dialog you are seeing with the Continue button as a choice is a product of
the IDE, IIRC, and once your product is deployed, your users won't get that
dialog.
(This from recollection and subject to correction).
 
You could wrap some of the code in a While statement and just keep looping
until you are successful.

I think you may need to review the architecture of your application. If you
have a process that can fail, but a user should be able to retry, that
process should be encapsulated in it's own procedure. The procedure should
return a success or failure code and the code that calls the procedure can
act on that by running it again, if necessary. (Well, that's one way to
handle it.)

With the event driven arhictecture, you can't really just stop in the middle
of a handler and wait for the user to try something new. You have to set it
up so that if you fail, you end up back where you started and wait for the
same event that triggered the failure. (Button click or whatever...)

Maybe if you posted mor of the code I can get a better idea of what you are
trying to do...

Jerry
 
Hi

What I am looking for is something very common to do in ERP
applications.
For instance, in an ERP I can write a function with something like
this:

<code>
get_customer_information();
get_account_information();
post_invoice();
message("OK");
</code>

This will read some data from the customer and account tables and will
write some data for posting the invoice.

If there is some problem reading the customer table, I'll throw an
error and I'm sure that the code execution will not reach the
post_invoice() function. For instance,

function get_customer_information(){
...
...

if customer.name = "" then
error("Customer is nameless!"); // <--- This is the function I'd
like

...

}


Of course I could code my functions in order to so something like

if get_customer_information() then
if get_account_information() then
if post_invoice() then
message("OK");


But this forces me to keep track of errors and exit boolean values in
every function, which is cumbersome when you have a fairly big
application. I am used to work with really big ERP systems where that
is simply not an option.

What I'm trying is to adapt my programming ideas from the ERP to VB
..NET, if it is possible.

Thanks!


Toni
 
What if you used a global error indicator, maybe called ErrorOccurred...

If any of the routines encounter an error, they pop up a message and set
ErrorOccurred = True in the Catch block.

Your parent routine calls the subs like this:

If Not ErrorOccurred Then Get_Customer_Information()
If Not ErrorOccured Then Get_Account_Information()
If Not ErrorOccured Then Post_Invoice()

Then at whatever point the error occurs, the rest of the routine is skipped
and your back where you started before this guy was called.

Other than this, I'm not sure I really understand what's missing.

Jerry
 
Back
Top