C # custom exception handling

Source: Internet
Author: User

In another article, I talked about custom exception handling in the. NET Exception Handling Mechanism. Today I want to talk about another exception handling method.

In the process of writing and processing program exceptions, different types of exceptions may occur, but different humanized prompts should be thrown. If the same prompts are thrown in a uniform manner, they will not be humanized,

Our general solution is:

  1. Public void Update ()
  2. {
  3. Try
  4. {}
  5. Catch (SqlExcetion ex)
  6. {
  7. Throw new excetion (ex );
  8. }
  9. Catch (IOExcetion ex)
  10. {
  11. Throw new excetion (ex );
  12. }
  13. // Other exceptions...
  14. Catch (Exception ex)
  15. {
  16. Throw new Exceptioin (ex );
  17. }
  18. }

Or use exception nesting (nesting exceptions in exceptions, so no code is available here)However, there is a problem with this method. If we have to handle it in multiple places, we will become a code worker. Therefore, use a class to manage it. Here we call it an exception handling class.
  1. /// <Summary>
  2. /// Global exception handling class
  3. /// Author: Database Home (http://www.uol123.com)
  4. /// </Summary>
  5. [Serializable]
  6. Public class ExceptioinManage: Exception
  7. {
  8. /// <Summary>
  9. /// Default constructor
  10. /// </Summary>
  11. Public parameter tioinmanage (){}
  12.  
  13. Public parameter tioinmanage (string message)
  14. : Base (message)
  15. {
  16. Throw new Exception (message );
  17. }
  18.  
  19. Public parameter tioinmanage (string message, Exception inner)
  20. : Base (message, inner ){}
  21.  
  22. Public ExceptioinManage (System. Runtime. Serialization. SerializationInfo info,
  23. System. Runtime. Serialization. StreamingContext context)
  24. : Base (info, context ){}
  25.  
  26. Public parameter tioinmanage (Exception ex)
  27. {
  28. If (ex. GetType () = typeof (System. Data. SqlClient. SqlException ))
  29. {
  30. // Log
  31. Throw new Exception ("the connection has been disconnected. Check the connectivity of the network !");
  32. }
  33. If (ex. GetType () = typeof (System. IO. IOExcetion) {// log throw new Exception ("file cannot be read! ");}
    // Other exceptions
  34. // Log (http://www.uol123.com)

  35. Throw ex; // default exception
  36. }
  37. }
In this way, we only need to change our throw new Excetion (ex) to throw new ExceptioinManage (ex) to throw a human-friendly exception! Fixed Link: c # custom exception handling (2) | Database Home + copy Link
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.