|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
won't catch my custom exceptioni 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 change last block to
catch (EmptyRecordException ex) { throw ex; } 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 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 > 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.csharpSent: 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 >> 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 > >> > 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 >> 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 > >> > > > rodchar wrote:
> "Hans Kesting" wrote: C# is not like C++ - you can only throw instances of Exception and>> 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, sub classes (or null) - you can not throw instances of an arbitrary type. Arne Because you should throw Exception , but not the Message of that Exception
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 >> |
|||||||||||||||||||||||