Solved

exception function/class needed

Posted on 2013-11-25
9
299 Views
Last Modified: 2013-11-29
Hi;

I need a plausible exception function or class in C#. That I can categorize, error, warning and information. Any code snippets for this?

Regards.
0
Comment
Question by:jazzIIIlove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39674824
Can you rephrase your question?
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39674991
Yes,

I need to use this implementation:
http://www.codeproject.com/Tips/90646/Custom-exceptions-in-C-NET

but how can i use this in my code?

Regards.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39675007
If you create a new class that derives from System.Exception (like the one in the example you linked to), then you simply need to throw a new instance of that exception wherever it would be appropriate to do so. For (a crude) example,

public void SomeMethod()
{
    // Do some stuff
    throw new CustomException("An error occurred!");  // Uh-oh

    // Some more stuff
}

Open in new window


So your method will be throwing this exception. You may decide you need to catch it elsewhere:

e.g.

public void SomeOtherCode()
{
    try
    {
        SomeMethod();
    }
    catch (CustomException ex)
    {
        Console.WriteLine(ex.Message);
    }
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39675022
my SomeMethod actually returns a string array in which i want to concatenate the error code with a timestamp. Do you think a custom exception is a feasible solution? how can I adapt it?

I mean there is throw new CustomException in your code but i cannot grab the exception with that, so how can i grab and return it in the string?

public string [] SomeMethod(string[] a)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39675062
Can you pseudo-code out what you'd like to do, and maybe I can better understand what it is you are after?
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39676449
Honestly, I believe that exceptions are what you need. It is against any rules to deliver information (not exceptional information) using exceptions.

I'd have a look at:
http://msdn.microsoft.com/en-us/library/system.diagnostics.traceswitch(v=vs.110).aspx

In our application I'm working on we use custom messaging system. We pass an instance of a message class to methods where something may happen. The class itself may have a list of erorrs (exceptions), warning and infos. After execution we check the 'message' for any erros etc and show them as required. No errors/warnings/infos - show nothing.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39676898
Hi;

Can you give a more detailed example?

regards.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39678590
Sorry for misprint: "... I believe that exceptions are NOT what you need. ".

I'll try to provide some example when I have time.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 39684382
Please see this small application. Attached is a zipped solution.

Of course, it's quite primitive but may give you an idea. Not sure if it suites you :).

Just in case - I re-uploaded file.
InfoMessages---Copy.zip
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question