Home All Groups Group Topic Archive Search About

won't catch my custom exception

Author
2 Jul 2009 2:00 PM
rodchar
hey all,

i have a very simple custom exception class:

    public class EmptyRecordException : Exception
    {
        string message;

        public override string Message
        {
            get
            {
                return message;
            }
        }

        public EmptyRecordException(string message)
        {
            this.message = message;
        }
    }


Here's how i try to throw it:

        public void ThrowMockException()
        {
            throw new EmptyRecordException("message from c2");
        }



i'm trying to catch it this way:

            catch (EmptyRecordException ex)
            {
                throw ex.Message;
            }

but it is giving me an error saying:
The type caught or thrown must be derived from System.Exception

any ideas?

thanks,
rodchar

Author
2 Jul 2009 2:09 PM
Armen Kirakosyan
change last block to

catch (EmptyRecordException ex)
            {
                throw ex;
            }



--

MCPD, MCT
http://www.explorer.am - Explore Armenia!



Show quoteHide quote
"rodchar" <rodc***@discussions.microsoft.com> wrote in message
news:52D335AB-5128-45E4-8DAE-C113AAAE7FE5@microsoft.com...
> hey all,
>
> i have a very simple custom exception class:
>
>    public class EmptyRecordException : Exception
>    {
>        string message;
>
>        public override string Message
>        {
>            get
>            {
>                return message;
>            }
>        }
>
>        public EmptyRecordException(string message)
>        {
>            this.message = message;
>        }
>    }
>
>
> Here's how i try to throw it:
>
>        public void ThrowMockException()
>        {
>            throw new EmptyRecordException("message from c2");
>        }
>
>
>
> i'm trying to catch it this way:
>
>            catch (EmptyRecordException ex)
>            {
>                throw ex.Message;
>            }
>
> but it is giving me an error saying:
> The type caught or thrown must be derived from System.Exception
>
> any ideas?
>
> thanks,
> rodchar
Are all your drivers up to date? click for free checkup

Author
2 Jul 2009 2:22 PM
rodchar
can i please ask why?

and does my custom exception class look correct?

Show quoteHide quote
"Armen Kirakosyan" wrote:

> change last block to
>
> catch (EmptyRecordException ex)
>             {
>                 throw ex;
>             }
>
>
>
> --
>
> MCPD, MCT
> http://www.explorer.am - Explore Armenia!
>
>
>
> "rodchar" <rodc***@discussions.microsoft.com> wrote in message
> news:52D335AB-5128-45E4-8DAE-C113AAAE7FE5@microsoft.com...
> > hey all,
> >
> > i have a very simple custom exception class:
> >
> >    public class EmptyRecordException : Exception
> >    {
> >        string message;
> >
> >        public override string Message
> >        {
> >            get
> >            {
> >                return message;
> >            }
> >        }
> >
> >        public EmptyRecordException(string message)
> >        {
> >            this.message = message;
> >        }
> >    }
> >
> >
> > Here's how i try to throw it:
> >
> >        public void ThrowMockException()
> >        {
> >            throw new EmptyRecordException("message from c2");
> >        }
> >
> >
> >
> > i'm trying to catch it this way:
> >
> >            catch (EmptyRecordException ex)
> >            {
> >                throw ex.Message;
> >            }
> >
> > but it is giving me an error saying:
> > The type caught or thrown must be derived from System.Exception
> >
> > any ideas?
> >
> > thanks,
> > rodchar
>
Author
2 Jul 2009 2:35 PM
Wolfgang Kluge
Hi,

the error is in the line:

  throw ex.Message;

Here you want to throw a System.String instead of an object of type
System.Exception...

greets and sorry for my bad english, Wolfgang Kluge
http://gehirnwindung.de/
http://klugesoftware.de/


----- Original Message -----
From: "rodchar" <rodc***@discussions.microsoft.com>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Thursday, July 02, 2009 4:22 PM
Subject: Re: won't catch my custom exception


Show quoteHide quote
> can i please ask why?
>
> and does my custom exception class look correct?
>
> "Armen Kirakosyan" wrote:
>
>> change last block to
>>
>> catch (EmptyRecordException ex)
>>             {
>>                 throw ex;
>>             }
>>
>>
>>
>> --
>>
>> MCPD, MCT
>> http://www.explorer.am - Explore Armenia!
>>
>>
>>
>> "rodchar" <rodc***@discussions.microsoft.com> wrote in message
>> news:52D335AB-5128-45E4-8DAE-C113AAAE7FE5@microsoft.com...
>> > hey all,
>> >
>> > i have a very simple custom exception class:
>> >
>> >    public class EmptyRecordException : Exception
>> >    {
>> >        string message;
>> >
>> >        public override string Message
>> >        {
>> >            get
>> >            {
>> >                return message;
>> >            }
>> >        }
>> >
>> >        public EmptyRecordException(string message)
>> >        {
>> >            this.message = message;
>> >        }
>> >    }
>> >
>> >
>> > Here's how i try to throw it:
>> >
>> >        public void ThrowMockException()
>> >        {
>> >            throw new EmptyRecordException("message from c2");
>> >        }
>> >
>> >
>> >
>> > i'm trying to catch it this way:
>> >
>> >            catch (EmptyRecordException ex)
>> >            {
>> >                throw ex.Message;
>> >            }
>> >
>> > but it is giving me an error saying:
>> > The type caught or thrown must be derived from System.Exception
>> >
>> > any ideas?
>> >
>> > thanks,
>> > rodchar
>>
Author
2 Jul 2009 2:45 PM
rodchar
thanks everyone for the help,
rod.

Show quoteHide quote
"Wolfgang Kluge" wrote:

> Hi,
>
> the error is in the line:
>
>   throw ex.Message;
>
> Here you want to throw a System.String instead of an object of type
> System.Exception...
>
> greets and sorry for my bad english, Wolfgang Kluge
> http://gehirnwindung.de/
> http://klugesoftware.de/
>
>
> ----- Original Message -----
> From: "rodchar" <rodc***@discussions.microsoft.com>
> Newsgroups: microsoft.public.dotnet.languages.csharp
> Sent: Thursday, July 02, 2009 4:22 PM
> Subject: Re: won't catch my custom exception
>
>
> > can i please ask why?
> >
> > and does my custom exception class look correct?
> >
> > "Armen Kirakosyan" wrote:
> >
> >> change last block to
> >>
> >> catch (EmptyRecordException ex)
> >>             {
> >>                 throw ex;
> >>             }
> >>
> >>
> >>
> >> --
> >>
> >> MCPD, MCT
> >> http://www.explorer.am - Explore Armenia!
> >>
> >>
> >>
> >> "rodchar" <rodc***@discussions.microsoft.com> wrote in message
> >> news:52D335AB-5128-45E4-8DAE-C113AAAE7FE5@microsoft.com...
> >> > hey all,
> >> >
> >> > i have a very simple custom exception class:
> >> >
> >> >    public class EmptyRecordException : Exception
> >> >    {
> >> >        string message;
> >> >
> >> >        public override string Message
> >> >        {
> >> >            get
> >> >            {
> >> >                return message;
> >> >            }
> >> >        }
> >> >
> >> >        public EmptyRecordException(string message)
> >> >        {
> >> >            this.message = message;
> >> >        }
> >> >    }
> >> >
> >> >
> >> > Here's how i try to throw it:
> >> >
> >> >        public void ThrowMockException()
> >> >        {
> >> >            throw new EmptyRecordException("message from c2");
> >> >        }
> >> >
> >> >
> >> >
> >> > i'm trying to catch it this way:
> >> >
> >> >            catch (EmptyRecordException ex)
> >> >            {
> >> >                throw ex.Message;
> >> >            }
> >> >
> >> > but it is giving me an error saying:
> >> > The type caught or thrown must be derived from System.Exception
> >> >
> >> > any ideas?
> >> >
> >> > thanks,
> >> > rodchar
> >>
>
Author
2 Jul 2009 2:42 PM
Hans Kesting
rodchar used his keyboard to write :
> can i please ask why?
>

The "it" that was giving the error apparently was the compiler (not the
runtime). And the line it was complaining about was not the declaration
of your exception, but the "throw ex.Message" statement.
And the compiler was complaining because Message is a string, not an
exception. So you can solve the issue by just using "throw ex".

> and does my custom exception class look correct?

Looks ok, but you don't need to override Message (unless you have other
plans). Rewrite the constructor as:

       public EmptyRecordException(string message)
          : base(message)
       {
       }

The ": base(message)" calls the base constructor, which will set the
Message property for you.

Hans Kesting


Show quoteHide quote
>
> "Armen Kirakosyan" wrote:
>
>> change last block to
>>
>> catch (EmptyRecordException ex)
>>             {
>>                 throw ex;
>>             }
>>
>>
>>
>> --
>>
>> MCPD, MCT
>> http://www.explorer.am - Explore Armenia!
>>
>>
>>
>> "rodchar" <rodc***@discussions.microsoft.com> wrote in message
>> news:52D335AB-5128-45E4-8DAE-C113AAAE7FE5@microsoft.com...
>>> hey all,
>>>
>>> i have a very simple custom exception class:
>>>
>>>    public class EmptyRecordException : Exception
>>>    {
>>>        string message;
>>>
>>>        public override string Message
>>>        {
>>>            get
>>>            {
>>>                return message;
>>>            }
>>>        }
>>>
>>>        public EmptyRecordException(string message)
>>>        {
>>>            this.message = message;
>>>        }
>>>    }
>>>
>>>
>>> Here's how i try to throw it:
>>>
>>>        public void ThrowMockException()
>>>        {
>>>            throw new EmptyRecordException("message from c2");
>>>        }
>>>
>>>
>>>
>>> i'm trying to catch it this way:
>>>
>>>            catch (EmptyRecordException ex)
>>>            {
>>>                throw ex.Message;
>>>            }
>>>
>>> but it is giving me an error saying:
>>> The type caught or thrown must be derived from System.Exception
>>>
>>> any ideas?
>>>
>>> thanks,
>>> rodchar
>>
Author
2 Jul 2009 4:21 PM
rodchar
thanks everyone for the extra insight, i appreciate it much,
rod.

Show quoteHide quote
"Hans Kesting" wrote:

> rodchar used his keyboard to write :
> > can i please ask why?
> >
>
> The "it" that was giving the error apparently was the compiler (not the
> runtime). And the line it was complaining about was not the declaration
> of your exception, but the "throw ex.Message" statement.
> And the compiler was complaining because Message is a string, not an
> exception. So you can solve the issue by just using "throw ex".
>
> > and does my custom exception class look correct?
>
> Looks ok, but you don't need to override Message (unless you have other
> plans). Rewrite the constructor as:
>
>        public EmptyRecordException(string message)
>           : base(message)
>        {
>        }
>
> The ": base(message)" calls the base constructor, which will set the
> Message property for you.
>
> Hans Kesting
>
>
> >
> > "Armen Kirakosyan" wrote:
> >
> >> change last block to
> >>
> >> catch (EmptyRecordException ex)
> >>             {
> >>                 throw ex;
> >>             }
> >>
> >>
> >>
> >> --
> >>
> >> MCPD, MCT
> >> http://www.explorer.am - Explore Armenia!
> >>
> >>
> >>
> >> "rodchar" <rodc***@discussions.microsoft.com> wrote in message
> >> news:52D335AB-5128-45E4-8DAE-C113AAAE7FE5@microsoft.com...
> >>> hey all,
> >>>
> >>> i have a very simple custom exception class:
> >>>
> >>>    public class EmptyRecordException : Exception
> >>>    {
> >>>        string message;
> >>>
> >>>        public override string Message
> >>>        {
> >>>            get
> >>>            {
> >>>                return message;
> >>>            }
> >>>        }
> >>>
> >>>        public EmptyRecordException(string message)
> >>>        {
> >>>            this.message = message;
> >>>        }
> >>>    }
> >>>
> >>>
> >>> Here's how i try to throw it:
> >>>
> >>>        public void ThrowMockException()
> >>>        {
> >>>            throw new EmptyRecordException("message from c2");
> >>>        }
> >>>
> >>>
> >>>
> >>> i'm trying to catch it this way:
> >>>
> >>>            catch (EmptyRecordException ex)
> >>>            {
> >>>                throw ex.Message;
> >>>            }
> >>>
> >>> but it is giving me an error saying:
> >>> The type caught or thrown must be derived from System.Exception
> >>>
> >>> any ideas?
> >>>
> >>> thanks,
> >>> rodchar
> >>
>
>
>
Author
3 Jul 2009 12:51 AM
Arne Vajhøj
rodchar wrote:
> "Hans Kesting" wrote:
>> rodchar used his keyboard to write :
>>> can i please ask why?
>> The "it" that was giving the error apparently was the compiler (not the
>> runtime). And the line it was complaining about was not the declaration
>> of your exception, but the "throw ex.Message" statement.
>> And the compiler was complaining because Message is a string, not an
>> exception. So you can solve the issue by just using "throw ex".
> thanks everyone for the extra insight, i appreciate it much,

C# is not like C++ - you can only throw instances of Exception and
sub classes (or null) - you can not throw instances of an arbitrary
type.

Arne
Author
2 Jul 2009 2:42 PM
Armen Kirakosyan
Because you should throw Exception , but not the Message of that Exception

--

MCPD, MCT
http://www.explorer.am - Explore Armenia!



Show quoteHide quote
"rodchar" <rodc***@discussions.microsoft.com> wrote in message
news:88315DAF-82DE-4666-9527-446BF228D91E@microsoft.com...
> can i please ask why?
>
> and does my custom exception class look correct?
>
> "Armen Kirakosyan" wrote:
>
>> change last block to
>>
>> catch (EmptyRecordException ex)
>>             {
>>                 throw ex;
>>             }
>>
>>
>>
>> --
>>
>> MCPD, MCT
>> http://www.explorer.am - Explore Armenia!
>>
>>
>>
>> "rodchar" <rodc***@discussions.microsoft.com> wrote in message
>> news:52D335AB-5128-45E4-8DAE-C113AAAE7FE5@microsoft.com...
>> > hey all,
>> >
>> > i have a very simple custom exception class:
>> >
>> >    public class EmptyRecordException : Exception
>> >    {
>> >        string message;
>> >
>> >        public override string Message
>> >        {
>> >            get
>> >            {
>> >                return message;
>> >            }
>> >        }
>> >
>> >        public EmptyRecordException(string message)
>> >        {
>> >            this.message = message;
>> >        }
>> >    }
>> >
>> >
>> > Here's how i try to throw it:
>> >
>> >        public void ThrowMockException()
>> >        {
>> >            throw new EmptyRecordException("message from c2");
>> >        }
>> >
>> >
>> >
>> > i'm trying to catch it this way:
>> >
>> >            catch (EmptyRecordException ex)
>> >            {
>> >                throw ex.Message;
>> >            }
>> >
>> > but it is giving me an error saying:
>> > The type caught or thrown must be derived from System.Exception
>> >
>> > any ideas?
>> >
>> > thanks,
>> > rodchar
>>

Bookmark and Share