tags:

views:

339

answers:

10

I've recently started a new project in C#, and, as I was coding some exception throw in a function, I figured out I didn't really know which exception I should use.

Here are common exceptions that are often thrown in many programs :

Are there any framework exceptions you often use in your programs ? Which exceptions should every .net programmer know about ? When do you use custom exception ?

EDIT : In order to clarify the topic, the original question was more about "which exception can I throw ?" than "what kind of exceptions should I catch ?".

+2  A: 

StackOverflowException

klausbyskov
A: 

DivideByZeroException

int SafeDivision(int x, int y)
{
    try
    {
        return (x / y);
    }
    catch (System.DivideByZeroException dbz)
    {
        System.Console.WriteLine("Division by zero attempted!");
        return 0;
    }
}
Filip Ekberg
+7  A: 

There are still some missing from your list.

This link is a good addition to your list :

msdn common Exceptions

Common Exception Types

djerry
Thanks for the links. That's what I was looking for.
Thibault Falise
+2  A: 

I use a custom exception when i have to raise an exception with some logic that is specific to the logic in the application, not the framework.

This means if my business layer receives a value that is not correct for the piece of functionality being executed, then i raise a custom exception. If the user is attempting to do something with a database record that my business rules forbid, then that is also a good candidate for a custom exception.

Basically you create custom exceptions to differentiate your application or business specific exceptions from the regular system exceptions. Your custom exceptions should still derive from System.Exception. The benefit you gain from using them is that you can craft code that catches them and take specific actions - you can't always take specific actions when you have a random System.Exception due to faulty logic or bugs.

slugster
I think it's better to derive from ApplicationException rather than Exception. Then you can differentiate between ApplicationException and SystemException (which are the ones the Framework throws...).
Carles
@Carles Microsoft are no longer recommending deriving from ApplicationException: http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=56 etc
Rob Fonseca-Ensor
+2  A: 

IndexOutOfRangeException

Thrown when an attempt to index an array via an index that is less than zero or outside the bounds of the array. -MSDN

TheMachineCharmer
I think, that if you are trying to deal with this exception, your code isn't perfect. Simple check at the begining that array is null or empty should avoid this exception.
dario
+2  A: 

I'm sure you have to know about every built-in exception class. You should know which exceptions you can throw and which not. You should understand how .net framework treats built-in exception. You should know when you had better inherit exist class and when define you own type. There are many predefined exceptions and almost always you can find appropriate one.

I'd recommend you to read about it in Jeffrey Richter's book.

alga
A: 

these exceptions are used in different purpose.

Xulfee
A: 

OutOfMemoryException

You need to know that why this kind of exception happens and why you should not try to handle it.

Lex Li
You actually throw an OutOfMemoryException yourself? In what cases?
David Rutten
just edit a little bit.
Lex Li
A: 

ThreadAbortException, because of the weird way it resurrects itself after each catch block

Rob Fonseca-Ensor