Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What is the best practice for exception management and error handling c#

What is the best practice for exception management and error handling c#

Scheduled Pinned Locked Moved C#
helpquestioncsharptutorialdiscussion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks

    tbhattacharjee

    L G J H 4 Replies Last reply
    0
    • T Tridip Bhattacharjee

      whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks

      tbhattacharjee

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You are not going to learn how to be a good programmer just by posting questions here. You need to go and do some studying, and then practice what you have learned. I already gave you a link to Charles Petzolds's book, which will give you a good start on the basics of .NET and C#; you really should read it. You can then follow that with some more advanced books which you can discover through Google.

      1 Reply Last reply
      0
      • T Tridip Bhattacharjee

        whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks

        tbhattacharjee

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #3

        The answer to the question is always throw exceptions. The only exception I can think of is some file handling situations where you can get an exception - but as previously mentioned the best thing to do is read up about it and use google. As a general tip you should not handle exceptions as by handling an exception you are running the risk of corrupting any data in your system.

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        1 Reply Last reply
        0
        • T Tridip Bhattacharjee

          whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks

          tbhattacharjee

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Tridip Bhattacharjee wrote:

          what is the best industry standard for exception management and error handling technique?

          It depends on the situation. An exception is supposed to represent an exceptional condition within the API that is being used. The caller is then responsible for determining what that exception means - to the caller. Thus the caller can catch it and do something or not catch it all. That is a general answer but it is further modified by what the caller is supposed to be doing. For example it is inappropriate to allow a SQL syntax error to bubble up to a UI to leave the consumer user to deal with it. But on the other hand one must tell the user that what they were trying to do didn't work. That means somewhere in the middle something must catch that exception, do something with it, and in some way relay a different error message to the user. A common way of dealing with exceptions is to use a logging api and log it. Like everything that requires moderation and thought as well since one should probably also log other, non-exceptional information and also not log every exception because some are in fact expected (thus generally pointless to log.)

          1 Reply Last reply
          0
          • T Tridip Bhattacharjee

            whenever i wrote code and if i smell that any error or exception may occur at run time then i use those code in try {} catch {} block and show exception related message from catch block but i like to what is the best industry standard for exception management and error handling technique? i like to show a friendly message to user as a result any lay man could understand what problem is occurring. guide me with best practice for exception management and error handling. thanks

            tbhattacharjee

            H Offline
            H Offline
            HKHerron
            wrote on last edited by
            #5

            Well, not to mention what has already been said, but when I first started learning, I would just download OPEN SOURCE projects i could find on the internet, and just read their code. See how they figured things out and how they handled certain things throughout their application. After doing this to as many as 20 - 30 projects, I really started to get the feel of what is COMMON PLACE and what is not. AS far as your question, i think you answered it yourself:

            Tridip Bhattacharjee wrote:

            i use those code in try {} catch {} block and show exception related message

            As that is what those are meant to be used for. The bigger question is "when should you use them?":confused:

            I Fart Ideas every day! It's just only once in a while one does not stink! :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups