C++ 和Java中捕获基类和派生类作为异常

C++ 和Java中捕获基类和派生类作为异常

在深入讨论我们的主题之前,让我们了解异常和异常处理是什么。异常是在我们编程过程中发生的错误,通常被视为不希望的错误或为了更好地理解。它们就像在编程过程中造成的障碍,异常处理是处理异常或发生的限制的错误处理机制。

处理异常处理和基类和派生类捕获作为异常的各种方法在C ++和Java编程语言中。

  • 在基类和派生类被声明为异常的机制中捕获的情况下,我们必须在显示输出终端上看到所述派生类的catch块出现在我们声明的基类类型之前。
  • 上述陈述是有效的,我们甚至可以重新检查,如果我们将基类放在派生类之前,那么我们声明的派生类将永远不会被执行。

算法(在C ++和Java中捕获基类和派生类作为异常)

示例:

begin //code starts from here
   declare a class b.
   say another class d which inherits class b.
   now display an object of class d.
   Try: throw derived.
   Catch (d derived)
      Print "code has been caught in the derived exception".
   Catch (B b)
      Print "code has been caught in the base exception".
End

C++ 代码

//here, we are writing down the C++ programming language code to
//demonstrate the concept of Catching Base and 
//Derived Classes as Exceptions in both C++ and Java languages
#include 
using namespace std;
class base_ {
};
class derived_: public base_ {
};
int main()
{
    derived_ d;
    //these are some necessary functionalities we may use
    try {
        //this is the monitored code we use
        throw d;
    }
    catch (base_ b) {
        cout << "OH! Caught Based Exception occured";
    }
    catch (derived_ d) {
        //note: the catch block we wrote below will never get executed!
        cout << "oh! caught in the derived exception...";
    }
    getchar();
    return 0;
}

输出:

prog.cpp: In function 'int main()':
prog.cpp:20:5: warning: exception of type 'derived_' will be caught
    catch (derived_ d) {
    ^
prog.cpp:17:5: warning:    by the earlier handler for 'base_.'
    catch (base_ b) {
/ tm  p /vLt kM  pH G zo.o
OH! Caught Based Exception occurred

C++ 代码

//here, we are writing down the C++ programming language code to
//demonstrate the concept of Catching Base and 
//Derived Classes as Exceptions in both C++ and Java languages
#include 
using namespace std;

class base_ {};
class derived : public base_ {};
int main()
{
    derived d;
    //these are some necessary functionalities we may use
    try {

        //this is the monitored code we use
        throw d;
    }
    catch (derived d) {
        cout << "the code has been caught in the derived exception";
    }
    catch (base_ b) {
        cout << "the code has been caught in the base exception";
    }
    //this below code snippet is to read out the next possible character
    getchar(); 
    return 0;
}

输出:

/tmp /vL tk MpH  Gzo.o
the code has been caught in the derived exception

Java代码

//here, we are writing down the Java programming language code to
//demonstrate the concept of Catching Base and 
//Derived Classes as Exceptions in both C++ and Java languages
class base_ extends Exception {
}
class derived_ extends base_ {
}
public class Main{
    public static void main(String args[])
    {
        try {
            throw new derived_();
        }
        catch (base_ b) {
        }
        catch (derived_ d) {
        }
    }
}

输出:

/tmp /vL tk MpH  Gzo.o
./Main.java:13: error: exception Derived has already been caught
        catch (Derived d) {
        ^
1 error

Java代码

//here, we are writing down the Java programming language code to
//demonstrate the concept of Catching Base and 
//Derived Classes as Exceptions in both C++ and Java languages
class base_b extends Exception {}
class derived_d extends base_b {}
public class Main {
public static void main(String args[]) {
// below, we wrote the try block code snippet
try {
    //this is the monitored code we use
    throw new derived_d();
    }
    catch(base_b)    {
    System.out.println("the code has been caught in the base exception");
    }
    catch(derived_d) {
    System.out.println("the code has been caught in the derived exception");
    }
}
}

输出:

Main.java:12: error: exception Derived has already been caught
    catch(derived_d)  { System.out.println("the code has been caught in the derived exception");}

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程