exceptions and variable argument lists

This is a discussion on exceptions and variable argument lists within the C++ Programming forums, part of the General Programming Boards category; Okay...this is a strange question that maybe no one has really asked before, but we will see how it goes. ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    exceptions and variable argument lists

    Okay...this is a strange question that maybe no one has really asked before, but we will see how it goes.

    I was thinking about exceptions in C++ recently, because here in university in my programming course the professor recently covered them.

    If you have worked with exceptions in C++ before, then you know that we can do a "catch-all" kind of thing, in which case, if a certain exception was not caught, there is a way we can catch any type of exception that maybe we weren't expecting. This is done like so:

    Code:
    catch ( ... ) { cout << "Unknown exception caught." << endl; }
    Well, at the same time, we also know that in C++ we can have functions with variable argument lists (this might seem completely unrelated, but bear with me and I will get to my point). With variable argument lists, we can have a function with a dynamic number of arguments. One example is printf, which takes however many arguments you really feel like passing it.

    A function is written so:

    Code:
    void myFunction ( ... )
    {
    
    ..code contained here to grab the arguments and do stuff...
    
    }
    Okay, now I come to my question. I noticed that these two different things use the same operator ( the ... operator ). That spawned some thought.

    It came to my mind, without actually reading any official text about it, that the catch might work in the same way that a function using a VAL (Variable Argument List) works. Since the exception being caught at that point in time is technically unknown and unhandled, it would make sense that it would be passed to catch in the same manner that some variable number of arguments is passed to a function.

    So, my questions is: can the unknown exception that is passed to catch be retrieved and used just like variables in a VAL can be retrieved and used? You might say: why would this be useful? But I see some usefulness in it.

    Let's say you weren't expecting a certain type of exception to be thrown, but it gets thrown anyways, and so your "catch all" catch segment of code catches the thrown exception. You could just print out an informative error message and quit the program, but what if you wanted to actually see what the exception was that was thrown? Could you access the exception just like you access a variable in a VAL?

    What are yalls thoughts on this? Or if you know of any authoritative text on this subject, lead me to it!
    Last edited by DavidP; 10-30-2006 at 12:22 AM.
    My Website

    "Circular logic is good because it is."

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by DavidP
    It came to my mind, without actually reading any official text about it, that the catch might work in the same way that a function using a VAL (Variable Argument List) works.
    It doesn't, and what you want is completely impossible except in some extremely compiler-specific ways that usually aren't actually supported by the compiler itself and rely on ugly direct manipulation of memory based on knowledge of the compiler's internal workings, which may well be obsolete with the compiler's next version.
    In other words, don't. Just don't.

    it would make sense that it would be passed to catch in the same manner that some variable number of arguments is passed to a function.
    No, it would not. A catch block is not a function, and parameter passing and exception handling have nothing to do with each other.

    Could you access the exception just like you access a variable in a VAL?
    You have to know the types of the variable arguments in order to use them. But if you know the type of the exception, then you could just write a normal catch.

    Or if you know of any authoritative text on this subject, lead me to it!
    I recommend the C++ standard, which says absolutely nothing about recovering unspecified exceptions. The only thing you can do with them is rethrowing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    66
    There are things called "hardware exceptions", which are specific to windows platform. You can use Microsoft specific:

    Code:
    __try 
    {
       // guarded code
    }
    __except ( expression )
    {
       // exception handler code
    }
    And catch structureed exceptions that are part of Win32 (access voilations, etc).


    Usually

    Code:
    try
    {
      // guarded code
    }
    catch (MyException& m)
    {
      // handle my exception
    }
    catch(...)
    {
      // Unknown exception and structured exception caught
      // Access violations will be caught here on MS platform
    }
    The (...) in this case is specific to the exception clause and not related to the ... in variable arguements. If you need to pass multiple variables in an exception, MyException class above would contain the members needed for that tasks (arrays, lists, buffers, etc).

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    catch(...) catching SEH exceptions was considered a serious mistake in VC++6 and has been corrected in later versions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exception and global variable
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 01-28-2008, 07:12 PM
  2. Template exceptions
    By Verdagon in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2005, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21