简体   繁体   中英

Custom Exception Class - Why do we need it?

I was reading about custom exception.. This is what I did.

#include <iostream>
#include <exception>
#include <stdexcept>

using namespace std;

class myBase : public exception
{
public:
    myBase() {};
    virtual ~myBase(void) {};
    virtual const char* what() const { return "Hello, you have done a mistake"; }
};

class myDerived : public myBase
{
public:
    myDerived() {};
    virtual ~myDerived(void) {};
    const char* what() const { return "Hello, you have done a mistake in - derived class"; }
};

int main(void)
{
    try
    {
        throw myDerived();
    }
    catch (exception& e)
    {
        cout << e.what() << endl;
    }

    getchar();
    return 0;
}

I understood that, I can throw my custom class objects and can catch it. I don't understand the purpose of this. Can someone explain me why do we need custom exception class? Any practical examples will help me understanding the purpose of using custom exception classes.

Thanks.

in c++ you can throw any type you want, you don't need to inherit from std::exception . the ability to catch different types in different catch statement is useful because you can throw different exception types for different conditions. In this way you can properly handle different exeptional cases.

in this toy example, based on the variable one a different exception is thrown and handled in the catch statements. In this case, "string" is printed to stdout .

#include <iostream>

using namespace std;

int main(void)
{
    int one = 0;
    try
    {
        if(one)
            throw 1;
        else
            throw "string";
    }
    catch (int& i)
    {
        cout << i << endl;
    }
    catch (const char* s)
    {
        cout << s << endl;
    }

    return 0;
}

Lets say that there's also another user defined class that also inherits from class myBase :

class myDerived2 : public myBase {
public:
    myDerived2() {}
    virtual ~myDerived2() {}
};

and lets say that you have a try catch clause:

try {
  myDerived  A;
  myDerived2 B;
  ...
  /* to staff with both A and B */
  ...
} catch (std::exception& e){
  cout << e.what() << endl; 
}
  • In the above try - catch clause you restrict your self by treating exceptions of type myDerived and myDerived2 in identical way.

  • What if you wanted to treat throws of type myDerived and myDerived2 in a different way?

  • Then you would have to define a different try-catch clause:


try {
  myDerived  A;
  myDerived2 B;
  ...
  /* to staff with both A and B */
  ...
} catch (myDerived &d1) {
  /* treat exception of type myDerived */
} catch (myDerived &d2) {
  /* treat exception of type myDerived2 */
}
  • From the above example I think it's clear that defining custom exceptions gives the programmer a valuable and versatile tool for dealing with throws in a more versatile and specialized manner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM