Open In App

Comparison of Exception Handling in C++ and Java

Last Updated : 10 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Both languages use to try, catch and throw keywords for exception handling, and their meaning is also the same in both languages. 

Following are the differences between Java and C++ exception handling:

Java

C++

Only throwable objects can be thrown as exceptions. All types can be thrown as exceptions.
We can catch Exception objects to catch all kinds of exceptions. Because normally we do not catch Throwable(s) other than Exception(s). There is a special catch called “catch all” that can catch all kinds of exceptions.
A special block called finally is always executed after the try-catch block.  There is no such block in C++.
There are two types of exceptions – checked and unchecked. All exceptions are unchecked.
A special keyword throws is used to list exceptions that can be thrown by a function. The keyword throw is used to list exceptions that can be thrown by a function.
 Finding and handling the exception in Java is easier. Finding and handling the exception in C++ is quite difficult. 

The above points have been discussed in detail below:
1) In C++, all types (including primitive and pointer) can be thrown as exceptions. But in Java, only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be thrown as exceptions. For example, the following type of code works in C++, but similar code doesn’t work in Java.

CPP




// CPP Program to demonstrate all types (including primitive
// and pointer) can be thrown as exception.
#include <iostream>
using namespace std;
int main()
{
    int x = -1;
 
    // some other stuff
    try {
        // some other stuff
        if (x < 0) {
            throw x;
        }
    }
    catch (int x) {
        cout << "Exception occurred: thrown value is " << x
             << endl;
    }
    getchar();
    return 0;
}


Output

Exception occurred: thrown value is -1

2) In C++, there is a special catch called “catch all” that can catch all kinds of exceptions.  

CPP




// CPP Program to demonstrate catch all
#include <iostream>
using namespace std;
int main()
{
    int x = -1;
    char* ptr;
 
    ptr = new char[256];
 
    try {
 
        if (x < 0) {
            throw x;
        }
        if (ptr == NULL) {
            throw " ptr is NULL ";
        }
    }
    catch (...) // catch all
    {
        cout << "Exception occurred: exiting " << endl;
        exit(0);
    }
 
    getchar();
    return 0;
}


Output

Exception occurred: exiting 

In Java, for all practical purposes, we can catch Exception objects to catch all kinds of exceptions. Because normally we do not catch Throwable(s) other than Exception(s) (which are Errors) 

catch(Exception e){
 …….
}

3) In Java, there is a block called finally that is always executed after the try-catch block. This block can be used to do cleanup work. There is no such block in C++. 

Java




// Java Program to demonstrate creating an exception type
class Test extends Exception {
}
 
class Main {
    public static void main(String args[])
    {
 
        try {
            throw new Test();
        }
        catch (Test t) {
            System.out.println("Got the Test Exception");
        }
        finally {
            System.out.println("Inside finally block ");
        }
    }
}


Output

Got the Test Exception
Inside finally block 

4) In C++, all exceptions are unchecked. In Java, there are two types of exceptions – checked and unchecked
5) In Java, a new keyword throws is used to list exceptions that can be thrown by a function. In C++, there is no throws keyword, the same keyword throw is used for this purpose also.
6) In C++ if the exception isn’t caught then the exception handling subsystem calls the function unexpected(), which terminates the program or an application abnormally. If any exception arises in our C++ program then finding that particular exception is very time-consuming because in C++ unexpected() did not tell us that which type and on which line the exception has occurred.
But in Java, if the system-generated exception isn’t caught then the java runtime system(JVM) handover the exception object to the default exception handler, which basically prints the name, description, and on which line the exception has occurred. So, in Java finding and handling the exception is easier than in the C++ language. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads