Exception Handling Comparison in C++ and Java

Today we’ll see a comparison of exception handling in C++ and Java.

Exception handling is one of the important features in the programming world. It tells the compiler how to handle flaws in the program. Various programming languages have varied exception handling features.

Comparison

The basic try-throw-catch block remains the same in both Java and C++. A few differences are as follows:

 JavaC++
Doesn’t have a ‘catch all’ block.Has a ‘catch all’ block to catch all kinds of exceptions.
Throwable objects can only be thrown as exceptions.Any type – primitives, pointers, etc. can be thrown as an exception.
A ‘finally’ block executes after the try-catch block for clean up. This block executes irrespective of whether an exception is caught or not.There is no such block in C++.
The keyword ‘throws’ lists down the exceptions thrown by any function.The keyword ‘throw’ lists down the exceptions thrown by any function.
Checked and unchecked exceptions exist.Unchecked exceptions are the only type that exist.
All versions of Java support exception handling.Earlier versions did not support exception handling

Example

In C++:

#include <iostream>
using namespace std;
int main() 
{
   float random_number;
   cout<<"What could be the temperature of Switzerland now?";
   cin>>random_number;
   try 
   {
      if( random_number < -3 )
      {
         throw random_number;
      }   
   }
   catch (float exc ) 
   {
      cout << "Exception caught! " << exc <<" is less than -3 thats minimum"<<endl;
   }
   return 0;
}

Clearly, any type of exception can be thrown, and this wouldn’t work in Java.

Output:

What could be the temperature of Switzerland now?                                                                       
-4.5                                                                                                                    
Exception caught! -4.5 is less than -3 that's minimum

Catch all block in C++ is as follows:

#include <iostream> 
using namespace std; 
 
void func_test(int INTexc) 
{ 
  try{ 
    if(INTexc==2) throw INTexc; // throw int 
    if(INTexc==0) throw 'L'; // throw char 
    if(INTexc==1) throw 198.92; // throw double 
  } 
  catch(...) { // catch all exceptions 
    cout << "Exception caught! Please review the code\n"; 
  } 
} 
 
int main() 
{ 
  func_test(0); 
  func_test(1); 
  func_test(2); 
 
  return 0; 
}

Thus, the catch all block catches all kinds of exceptions thrown. This useful feature doesn’t exist in Java programming language.

Outcome:

Exception caught! Please review the code                                                                                
Exception caught! Please review the code                                                                                
Exception caught! Please review the code

In Java:

Finally block in Java is as shown:

public class Example_2{
  public static void main(String args[]){
    try{
       System.out.println("We're inside the try block");
       int random_number=34/17;
       System.out.println(random_number);
    }
    catch(ArrayIndexOutOfBoundsException exc){
       System.out.println("ArrayIndexOutOfBoundsException");
    }
    finally{
       System.out.println("Welcome to the finally block");
    }
    System.out.println("Coming out of all blocks");
  }
}

Result:

We're inside the try block
2
Welcome to the finally block
Coming out of all blocks

From the above code and output, it is evident that the ‘finally’ block executes irrespective of exceptions. This feature is exclusive for Java programming language.

To know more about each feature up can visit C++ reference link and Java reference link.

Leave a Reply

Your email address will not be published. Required fields are marked *