Exception Handling In C++ And Java - C++ Programming Tutorial
C++ Course / Exception Handling / Exception Handling In C++ And Java

Exception Handling In C++ And Java

BLUF: Mastering Exception Handling In C++ And Java is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Exception Handling In C++ And Java

C++ is renowned for its efficiency. Learn how Exception Handling In C++ And Java enables low-level control and high-performance computing in the tutorial below.

In C++, exceptions can be thrown for any type, including primitive types and pointers. On the other hand, Java only allows throwable objects, which are instances of subclasses of the Throwable class, to be thrown as exceptions. This means that while the given code snippet is valid in C++, it is not permissible in Java.

Example

// 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

In C++, there exists a unique concept known as the "catch all" that can handle exceptions of all types.

Example

// 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

Output

Exception occurred: exiting

For all practical purposes, we can use catch Exception objects in Java to catch all types of exceptions. Because we do not normally catch Throwable(s) other than Exception(s) (which are Errors)

Example

catch(Exception e){
 .......
}
  1. In Java, the finally block is executed regardless of whether an exception is thrown or not from the try-catch block. It is commonly employed for carrying out cleanup operations. Conversely, C++ lacks an equivalent block for this purpose.
Example

// 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

Output

Got the Test Exception
Inside finally block
  1. All exceptions in C++ are unchecked. There are two kinds of exceptions in Java: checked and unchecked. More information on checked vs. unchecked exceptions can be found here.
  2. In Java, a new keyword throws is used to list exceptions that a function can throw. There is no throws keyword in C++, so the keyword throw is used instead.
  3. If an exception is not caught in C++, the exception handling subsystem calls the function unexpected, which causes the programme or application to terminate abnormally. If an exception occurs in our C++ programme, locating that exception is time-consuming because C++ unexpected does not tell us which type or line the exception occurred on. Refer to this for more information on unexpected.

In Java, when an exception generated by the system is not caught, the Java Virtual Machine (JVM) transfers the exception object to the default exception handler. This handler then displays details such as the name, description, and line number of the exception occurrence. Consequently, the process of identifying and dealing with exceptions in Java is more straightforward compared to C++.

Exception Handling Java vs. C++: What's the Difference?

The variances between Java and C++ are outlined as follows.

JAVA C++
As exceptions, only throwable objects can be thrown. Exceptions of any type can be thrown.
We can use Exception objects to catch various types of exceptions. Because we do not normally catch Throwable(s) other than Exception (s).
A special catch known as "catch all" can catch all types of exceptions. After the try-catch block, a special block called finally is always executed. In C++, there is no such block.
Exceptions are classified into two types: checked and unchecked. All exceptions have been left unchecked.
Throws is a special keyword that is used to list exceptions that a function can throw. The keyword throw is used to specify which exceptions a function may throw.
In Java, finding and handling exceptions is simplified. In C++, finding and handling exceptions is a difficult task.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience