473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Custom Exception in C# windows application

Hello,
I have a c# windows application and I am using a try catch block to handle some exception.
I want to show show error in messagebox. but the problem is that program execution does not stop there.
If I use 'throw new exception', I get that ugly windows default msg box.

any solutions?

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3. ...code
  4. }
  5. catch (Exception)
  6. {
  7.     MessageBox.Show("Error: Invalid Parameters" + Environment.NewLine + "Enter correct values", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  8. }
Jun 23 '09 #1
5 3590
tlhintoq
3,525 Expert 2GB
What do you mean by
but the problem is that program execution does not stop there.
?

Are you saying you don't get an exception unless you explicitly throw it?

Maybe you could provide the actual code you are testing for inside the 'try' block.

You may also be interested in something like this...

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.    // Code that breaks goes here
  4. }
  5. catch(Exception err)
  6. {
  7.    MessageBox.Show(err.message, "error");//overload as you like
  8. }
  9.  
...where the box dynamically includes the actual error message.
Jun 23 '09 #2
here are the details:

1. the control comes to the catch block.
2. custom message appears on the screen.
3. when I press OK on the message window. the control moves though the catch block and executes other statements after that.

but I want that the program execution should stop after catch block statements are done.
Jun 23 '09 #3
tlhintoq
3,525 Expert 2GB
Oh... You can return out of the function.

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.    // Code that breaks goes here
  4. }
  5. catch(Exception err)
  6. {
  7.    MessageBox.Show(err.message, "error");//overload as you like
  8.    return;// Leave this method and return to where it was called.
  9. }
But might I suggest you read up on the ErrorProvider component? I sounds like that might be more along the lines of your final goal.
Jun 23 '09 #4
thanks.
i'll check on that

@tlhintoq
Jun 23 '09 #5
r035198x
13,262 8TB
@hsachdevah
That is exactly how the program should behave. Providing the catch block means that you have handled the exception so the program will not crash.
If you want to program to exit after the user has clicked OK then provide the exit statement in the catch block. If you want the method to stop executing at that point then put a return statement in that catch block.
Jun 24 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chris Dunaway | last post by:
I'm creating a Web service and a Windows Forms application to consume it. My question is about throwing a custom exception inside the WebService. Can that be done, and can the custom web service be...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
15
by: bill salkin | last post by:
I'd like to create a custom error handler like this in VB.NET: .... try ... Throw ("Lender Name not in table") .... catch ("Lender Name not in table")
6
by: Steve Amey | last post by:
Hi all I want to be able to throw a custom error up the call stack. I have looked around and it seems as though it's possible, but I can't get it to work :o( Below is some sample code. ...
5
by: Jon Skeet [C# MVP] | last post by:
I've run against a problem which I'm *sure* must be easy to solve - but I'm blowed if I can find the answer :( I have a web service which I want to require authentication. I need to authenticate...
1
by: David Herbst | last post by:
Enterprise Library Jan 2006 with Visual Studio 2005 on Windows 2000 Server sp4. My custom exception formatter fails with a "Unable to handle exception: 'LoggingExceptionHandler'." exception. ...
0
by: gilly3 | last post by:
I'm coming across all kinds of frustration implementing custom errors in ASP.NET 1.1. First, 401 - Authorization Failed My application uses Windows Integrated Authentication, and restricts...
0
by: GreggTB | last post by:
I have written two custom providers for our Oracle 9i database...a Membershp provider and a Role provider. I've been testing these over the last few days and they seem to be working well. However,...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.