Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching custom exception in c# [closed]

I have created a custom exception class as below

namespace testingEXception
{
    public class CustomException : Exception
    {            
        public CustomException()
        {
        }
        public CustomException(string message)
            : base(message)
        {
        }

        public CustomException(string message, Exception innerException)
            : base(message, innerException)
        {

        }
    }
}

I am throwing an exception from a different project in the same solution like this

namespace ConsoleApplication1
{
    public class testClass
    {
        public void compare()
        {
            if (1 > 0)
            {
                throw new CustomException("Invalid Code");
            }
        }
    }
}

and catching it like this

    namespace testingEXception
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                testClass obj = new testClass();
                obj.compare();

            }
            catch (testingEXception.CustomException ex)
            {
                //throw;
            }
            catch (Exception ex)
            {
               // throw new CustomException(ex.Message);
            }

            Console.ReadKey();
        }
    }
}

The problem is, the exception is not getting caught by the first catch, but instead getting caught by the second catch, over though the type of exception shows CustomException.

like image 417
Hataf Moin Avatar asked Aug 14 '13 16:08

Hataf Moin


People also ask

What is the point of catching an exception?

You're giving them the chance to either correct the issue, log the error, or pass the Exception up the chain until something useful can be done with it.

Where can I catch exceptions?

You should catch the exception when you are in the method that knows what to do. For example, forget about how it actually works for the moment, let's say you are writing a library for opening and reading files. Here, the programmer knows what to do, so they catch the exception and handle it.

What is catch C#?

catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception. finally − The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown.


1 Answers

You need to provide more detail, the following code outputs "CustomException":

try
{
    throw new CustomException("Invalid code.");
}
catch (CustomException ex)
{
    System.Diagnostics.Trace.WriteLine("CustomException");
    throw;
}
catch (Exception ex)
{
    throw;
}

With the following class:

public class CustomException : Exception
{
    public CustomException()
    {
    }
    public CustomException(string message)
        : base(message)
    {
    }

    public CustomException(string message, Exception innerException)
        : base(message, innerException)
    {

    }
}

Update:

With regard to optimizations and optimizing away a throw: this cannot happen because any particular block of code cannot know whether a caller higher up in the stack could have code to catch CustomException. Throwing an exception is a visible side-effect and there are various guarantees in the CLI to ensure those visible side-effects remain visible.

In addition, try, catch and finally blocks are "protected regions" in CLI-speak. These regions are special in that the operations within the region with "visible" side-effects cannot have their visible side-effects re-ordered. For some more detail, see http://lynk.at/qH8SHk and http://lynk.at/pJcg98

like image 165
Peter Ritchie Avatar answered Oct 19 '22 13:10

Peter Ritchie