Thread: Exception handling

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    Exception handling

    Hello

    Can someone explain me why exception handling is used?
    I have the following example:
    Code:
    #include "Matrix.h"
    #include "iostream"
    
    using namespace std;
    
    class CInvalidArgumentsException{};
    
    
    int main(void){
    
    	CInvalidArgumentsException e;
    
    	try{
    		Matrix matrix1 = Matrix(10,10);
    
    		if(test something)
    			throw(e);
    		else (do something)
    
    	}catch(CInvalidArgumentsException){
    		
    		cout << "error\n";
    	}  
    
      return 0;
    }
    I know how it works, you give an id to the constructor from the class: CInvalidArgumentsException and generate an error that you ask in the catch.

    What i'm not sure of is, is it necessary to always test in the try catch block with an if test to trow the exception? If this is the only way is it not easier to print the error out in the if test?
    I am used to programming in C.

    Thank you

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This use of exceptions is rather pointless. You'd generally throw an exception if you don't know how to handle it in the current function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >>What i'm not sure of is, is it necessary to always test in the try catch block with an if test to trow the exception? If this is the only way is it not easier to print the error out in the if test?
    You could technically throw anywhere and even propagate an exception with a throw; statement.

    I take exceptions -- the concept -- as a way not to crash if one of my constraints is broken. If we take a look at standard code, then you will find that operator new throws bad_alloc if allocation failed, or at() throwing an out_of_range exception at times. These are normally program restraints that you should check at one time or another, and that would indeed involve at least an if statement. A program constraint is a rule based in the problem that you are solving that should never be broken.

    OTOH, program constraints are not really errors that you expect to handle. The acceptable way to handle broken constraints might be writing to an error log before return 0; in the main() function. If you take that particular example in your post and make it into a convention, then yes, that is basically a replacement for error codes, but that is not normally how or why exceptions are used.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If we take a look at standard code, then you will find that operator new throws bad_alloc
    Which in all of MSVS's pursuits of the standard have yet to implement this as default behavior.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thank you all for your responses.

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. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM