开发者

exception derived from system.exception thrown but not caught

开发者 https://www.devze.com 2023-02-03 12:46 出处:网络
I am using the following system.exception derived exception in the following manner and would expect the exception thrown in SomeFunction to be caught in the catch block in SomeOtherFunction but inste

I am using the following system.exception derived exception in the following manner and would expect the exception thrown in SomeFunction to be caught in the catch block in SomeOtherFunction but instead I rece开发者_Go百科ive an unhandled exception error when the code attempts to throw the exception, any insight would be appreciated.

void SomeFunction()
{
   if (someValue == false)
   {
      throw(new MyException("some error, falseValue,null);
   }
}

void SomeOtherFunction()
{
    try
    {
       SomeFunction();
    }
    catch(MyException ex)
    {
    }
}

public class MyException : Exception
{
    public MyException (string message, Reason reason, Exception innerException)
        : base(message, innerException)
    {
        Reason = reason;
    }

    public Reason Reason { get; private set; }
}


There must be other code that is contributing to this issue - here is a complete example that works:

using System;

class Example
{
    static void Main()
    {
        SomeOtherFunction();
    }

    static void SomeFunction()
    {
        throw new MyException("some error");

    }

    static void SomeOtherFunction()
    {
        try
        {
            SomeFunction();
        }
        catch (MyException)
        {
            Console.WriteLine("caught the exception");
        }
    }
}

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

Perhaps you can use this example and work backwards to see if there is something missing from your real code? Since there were compilation issues with your examples I am assuming that you have sanitized some production code.


I see some minor syntax errors. But I think your unhandled exception error might be coming from the constructor of MyException or the way you're handling Reason in your constructor/get,set methods. It seems like you're trying to set a value to a class, but I'm still trying to migrate from c++..so idrk what im talking about. Try fixing syntax and working with your "Reason"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号