By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,785 Members | 2,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,785 IT Pros & Developers. It's quick & easy.

Stopping error message in VB .NET

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Toni" <to************@gmail.com> wrote in message
news:1d**************************@posting.google.c om...
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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Jul 22 '05 #2

P: n/a
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
"Toni" <to************@gmail.com> wrote in message
news:1d**************************@posting.google.c om...
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

Jul 22 '05 #3

P: n/a


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

Jul 22 '05 #4

P: n/a


to************@gmail.com ha escrito:
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

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

Jul 22 '05 #5

P: n/a

<to************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

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).

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Jul 22 '05 #6

P: n/a
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

"Jerry Camel" <rl*****@msn.com> wrote in message
news:O6****************@tk2msftngp13.phx.gbl...
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
"Toni" <to************@gmail.com> wrote in message
news:1d**************************@posting.google.c om...
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


Jul 22 '05 #7

P: n/a
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

Jul 22 '05 #8

P: n/a
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
<to************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.