Results 1 to 7 of 7

Thread: newbie..Exception Handling

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203

    newbie..Exception Handling

    here's a code I gleaned frrom a book...

    #include<iostream.h>

    class Bummer{};
    class Dummer{};
    class Dumm{};
    void main()
    {
    try{

    throw Dumm();
    }
    catch(Bummer)
    {
    cout<<"helo Bumer";
    }
    catch(Dummer)
    {
    cout<<"helo Dummer";
    }
    catch(...)
    {
    cout<<"helo rest of u";
    }

    }


    what I wanna know is tt y is it necessary 2 create these Classes ..Bummer Dummer..I mean..y cant v just say sumthing lik

    throw(5)

    catch(int i)
    {
    select case i
    {
    //coding here...
    }

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It's not nessesary, you can use it for simple error codes, but if you want to have an error container that stores additional data about what happened, say if you have an error class filenotfound, then you can store it's filename and in the catch you display the filename that wasn't found.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Anything can be thrown, but the best thing to throw is a class derived from the standard library's exception class. For example:
    Code:
    #include <iostream>
    #include <stdexcept>
    
    using std::cout;
    using std::endl;
    
    int main() {
        try {
            throw std::logic_error("Exception");
            throw std::bad_alloc;
        } catch(std::exception &ex) {
            cout << ex.what() << endl;
        }
    
        return 0;
    }
    I passed a reference in the exception handler because that means you can catch anything derived from std::exception, and still be able to get the proper information off it.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    what if you made something like this:

    template <const char* T>class Exception{};
    ...
    try{
    throw Exception<"File not found">();
    }
    catch(Exception<"File not found">){
    }
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That won't work unless the compiler optimises the two strings down to exactly the same pointer (since they're the same string - it's the "Eliminate Duplicate Strings" option in MSVC).

    And how do you catch any kind of Exception object?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    just regular inheritance, btw it didn't work :s
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Didn't think it would - anyway, for exception handling virtual inheritance is perfectly okay. The performance aspect goes pretty much out of the window in an exceptional case.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width