Thread: Confused about Exception handling

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    Confused about Exception handling

    Code:
    void x :: setValues (unsignedshort m_headLight) throw (OutOfRangeException)
    {
    	try
    	{
    		if (m_headLight > 1)
    		{
    			throw OutOfRangeException (WARNINGEXP, "'m_headLight' is out of range", "Value should be either 0 or 1", __FILE__, __LINE__);
    		}
    	}
    	d_y.m_headLight = m_headLight;
    }
    The above code will work even if I remove the following statement in RED color
    Code:
    void x :: setValues (unsignedshort m_headLight) throw (OutOfRangeException)
    Then What is the purpose of writing that statement ? Kindly explain !

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It used to be some sort of method to tell the compiler that a function throws a specific object. The compiler could then also enforce that only that kind of exception would be thrown from the function.
    Unfortunately, this isn't really the case. The exception specifier is hardly used, or implemented, at all. It would be better to simply ignore it, unless you want it there for decorative purposes (such as saying this function will throw only these objects; though you don't actually need to enforce it).

    In summary, the red part is not needed. It is an optional add-on to give the compiler extra information, but not all compilers enforce it, and even if they do, they enforce it at runtime.
    It is not a requirement to have it to throw an exception.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Elysia

    Many thanks to you for the enlightening reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Exceptions.h file during build of QuickFix library
    By arupsarkar in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2010, 10:30 AM
  2. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  3. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  4. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  5. is such exception handling approach good?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 08:54 AM