4 Replies - 451 Views - Last Post: 20 May 2019 - 07:35 AM Rate Topic: -----

#1 BassemK   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 06-March 19

how to override all connection exceptions (Throughout the program) wit

Posted 16 May 2019 - 05:30 AM

hi, i write a C# program (Windows form) that work online , but many exceptions appears because of slow internet.
how to override all connection exceptions (Throughout the program) with another custom msg and show C# MessageBox.Show.


i tried throw custom exception like this , but i want to write class that affect on all the program and appear automatically if any connection exceptions occurs.
try
        {               
            newStudent = new Student();
            newStudent.StudentName = "James007";
            
            ValidateStudent(newStudent);
        }
        catch(InvalidStudentNameException ex)
        {
            Console.WriteLine(ex.Message );
        }


Is This A Good Question/Topic? 0
  • +

Replies To: how to override all connection exceptions (Throughout the program) wit

#2 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: how to override all connection exceptions (Throughout the program) wit

Posted 16 May 2019 - 06:35 AM

You're not throwing any exceptions, you are catching one of them. Which has nothing to do with connectivity nor is it a valid exception. Post the relative code to your Student class. It's very unclear what your problem is at this point. The only way "slow internet" will cause you problems, is if you're using sockets or connecting to some form of online resource. Please clarify your post with more info.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15105
  • View blog
  • Posts: 60,392
  • Joined: 12-June 08

Re: how to override all connection exceptions (Throughout the program) wit

Posted 16 May 2019 - 07:07 AM

Why would you want to do that?
What's wrong with the current exception class/info?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,665
  • Joined: 05-May 12

Re: how to override all connection exceptions (Throughout the program) wit

Posted 16 May 2019 - 06:57 PM

You could take advantage of AppDomain.FirstChanceException or AppDomain.UnhandledException . In either case, there is very little you can do to actually handle the exception, it's just purely a notification mechanism to allow you to do some kind of logging or reporting. So this maybe perfect for your stated need of displaying a message box, but after that message box is shown, the normal exception handling will still continue to happen.
Was This Post Helpful? 0
  • +
  • -

#5 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1399
  • View blog
  • Posts: 1,988
  • Joined: 24-August 11

Re: how to override all connection exceptions (Throughout the program) wit

Posted 20 May 2019 - 07:35 AM

It sounds like you want every Exception thrown by your app that is related to network connectivity issues to automatically show a message (either Exception.Message or some other message you define), and you want to do that by creating a custom Exception implementation that does the work for you. If that's not the case, please elaborate further.

What you're proposing doesn't match up with what an Exception is or how it works within the framework, and architecturally it's a poor place to do that work. In short - that's just not how you solve that problem. Think of an Exception like a wrapper for an error - it provides a little context about what went wrong and the state of the app when it happened, and perhaps more importantly provides a mechanism (in conjunction with a Try/Catch or Try/Catch/Finally block) for temporarily halting execution to allow for recovery or graceful failure. It is not a means for communicating with the user - that responsibility lies solely with the UI - and it is not a mechanism for making decisions about how to proceed - that responsibility lies further up the invocation chain.

Try/Catch is the appropriate way to do the work you're looking for. Yes, you'll end up having multiple (maybe many) MessageBox.Show() or Console.WriteLine() calls, but it's supposed to be like that. Whenever you end up catching an exception, the app is potentially in an unstable state and you should be taking great care to ensure that you're taking the appropriate steps to either recover or fail gracefully, and then notify the user that something went wrong.

In your example, you reference a custom InvalidStudentNameException. That doesn't sound like a connectivity issue, that sounds like a valid scenario where an invalid student name was provided. If that exception could also describe a scenario where a connection timed out, then it has exceeded the scope for which it was defined. There are many different exceptions that an app could throw, but you don't manage that by wrapping them all in a single uber-exception class and then forcing the UI to work out what the problem is. What type/types of network communication is your app doing?

This post has been edited by h4nnib4l: 20 May 2019 - 08:01 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1