Exception Handling in C++ vs Java
Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required in both languages for managing exceptions, which is a comparable need for both C++ and Java. The terms to try, catch, and block are used for exception handling in both languages, and they also have the same meaning in both languages. Even so, there are certain challenges.
The following points outlines how Java and C++ handle exceptions differently :
Java :
- As an exception, only throwable objects may be hurled.
- To handle any error, we may capture Exception objects. Because we typically only catch Exception-related Throwable(s).
- After the try-catch block, an additional special block known as finally is always carried out.
- Checked and unchecked exceptions are the two different sorts.
- In order to list the exceptions that a function may throw, use the special term throws.
- In Java, finding and managing the exception is simpler.
C++ :
- Any type may be raised as an exception.
- All sorts of exceptions can be caught with an unique catch called "catch all."
- Such(finally) a block doesn't exist in C++.
- There are no exceptions selected.
- The exceptions which could be thrown by a function are listed using the term throw.
- In C++, locating and managing the exception is fairly challenging.
Following is a detailed discussion of the aforementioned points:
1) In C++, exceptions can be thrown for any type, including primitive and pointer types. But only throwable objects—which are instances of any Throwable class subclass—can be thrown as exceptions in Java. For instance, the given type of code is valid in C++ but invalid in Java.
Example :
// CPP Program for illustrating all the types (like primitive
// and pointer) could be thrown as exceptions.
#include <iostream>
using namespace std;
int main()
{
int a = -5;
// some other kind of stuff
try {
// some other kind of stuff
if (a < 0) {
throw a;
}
}
catch (int a) {
cout << "Exception has occurred : the thrown value is " << a
<< endl;
}
getchar();
return 0;
}
Output :
Exception has occurred : the thrown value is -5
2) The "catch all" function in C++ is a particular catch all method that can handle any exception.
Example :
// CPP example to illustrate catch all functions
#include <iostream>
using namespace std;
int main()
{
int a = -43;
char* pntr;
pntr = new char[256];
try {
if (a < 0) {
throw a;
}
if (pntr == NULL) {
throw " the pntr is NULL ";
}
}
catch (...) // catch all function
{
cout << "Exception has occurred: now exiting " << endl;
exit(0);
}
getchar();
return 0;
}
Output :
Exception has occurred: now exiting
For all intents and purposes, we can capture exceptions in Java using Exception objects. Because we typically just catch Exception(s) and Throwable(s) (which are Errors)
catch(Exception exp){
//something
}
3) After the try-catch and block in Java, a block known as finally is always run. Cleaning up may be done with this block. Such a block doesn't exist in C++.
Example :
// Java Programme to illustrate the creation of an exception type
class Testing extends Exception {
}
class Main {
public static void main(String argmnts[])
{
try {
throw new Testing();
}
catch (Testing x) {
System.out.println("Received the Testing Exception");
}
finally {
System.out.println("\nInside the finally block ");
}
}
}
Output :
Received the Testing Exception
Inside the finally block
4) All exceptions in C++ are unchecked. Checked and unchecked exceptions are both available in Java.
5) A new term called throws is used in Java to list the exceptions that a function may throw. There exists no throws keyword in C++, instead, the term throw is used in this context.
6) In C++, if the exception isn't handled, the function unexpected() is called, which causes an improper programme or application termination. Finding a specific exception that occurs in our C++ software takes a lot of effort since unexpected() in C++ does not inform us what type or line the exception has happened on. In contrast, Java's runtime system (JVM) hands away the exception object to the default exception handler if the system-generated exception isn't handled. This default exception handler merely publishes the name, description, and line number where the error occurred. Therefore, it is simpler to identify and handle exceptions in Java than it is in C++.