Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)

Can I replace "throw new Exception" with a custom popup without causing problems in the program?

I want to replace this:

throw new Exception("I show my message.");

with this;

this.Alert("I show my message", FAlert.enmType.Info);


What I have tried:

I know it's possible, but I don't know if it will cause any problems or bugs.
Updated 13-Jun-23 9:36am
Richard Deeming 13-Jun-23 7:45am    
We cannot see your screen, access your computer, or read your mind. There is no way anyone can tell you what impact that change would have on your code, since we have no idea what your code looks like.
CHill60 13-Jun-23 8:20am    
You've tagged this as WinForms but Form does not have an Alert method. When you edit your question to actually add some code, be sure to edit the tags with the correct platform as well
[no name] 13-Jun-23 8:26am    
Do you want modal or modeless?
PIEBALDconsult 13-Jun-23 14:53pm    
And nasal fitment?

The big difference between the two is not the way the message is displayed. It's how control flows afterward. The first will immediately (excluding a finally clause) exit the method after the throw and keep propagating that immediate-exit functionality back up the call stack until some method catches the exception. The second ... assuming Alert just displays a message or a notification ... will continue executing in the current method, starting with the next line of code after the Alert call.

Changing how your method exits may have huge side effects throughout your code. First, you must check the current method and see how it will handle executing after whatever state caused the exception. It probably wasn't designed to continue executing. After fixing that method to deal with whatever situation got you into the exception state, you'll have to check each and every one of the calling methods, and determine how you'll need to modify its error handling.
Share this answer
If this is Winforms as you tagged, you could try using MessageBox Class (System.Windows.Forms) | Microsoft Learn[^]

If this is web based, then you need to send a HTML / Javascript Alert to the client browser: they cannot see anything that happens on the server and that is where all the C# code will execute.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900