Exception Handling in C++ vs Java



In both languages, exception handling is used to identify errors using the try, catch, and throw keywords.

Exception handling is a programming mechanism that works with errors and unexpected events that occur during program execution.

Exception Handling in C++

C++ provides an inbuilt feature for handling exceptions using try and catch block. It is an exception-handling mechanism; the code that has an exception is placed inside the try block, and the code that handles the exception is placed inside the catch block. Visit here to learn more about C++ Exception.

Syntax

Following is the syntax of C++ Exceptions:

try {         
   // code ...
} 
catch (ExceptionType e) {   
   // exception handling code...
}

Implementation of Exception-Handling in C++

Following is the basic example to demonstrate the exception-handling in C++:

#include <iostream>
using namespace std;

double division(int a, int b) {
   if( b == 0 ) {
      throw "Division by zero condition!";
   }
   return (a/b);
}

int main () {
   int x = 50;
   int y = 0;
   double z = 0;
 
   try {
      z = division(x, y);
      cout << z << endl;
   } catch (const char* msg) {
     cerr << msg << endl;
   }

   return 0;
}

Following is the exception of the above code:

Division by zero condition!

Exception Handling in Java

In Java, exception handling allows developers to manage errors effectively using the try-catch block and the finally block (finally is an optional block). An exception in Java is an unwanted and unexpected error that occurs during the execution of the program. Visit here to learn more about Java Exception.

Syntax

Following is the syntax Java exception:

try {         
   // code ...
} 
catch (ExceptionType e) {   
   // exception handling code...
}

Implementation of Exception-Handling In Java

In this Java program, we demonstrate handling the exception using a try-catch block::

import java.io.*;
public class Division {
   public static void main(String[] args) {
      int a = 10;
      int b = 0;

      try {
         int ans = a / b;
         System.out.println("Answer: " + ans);
      } catch (ArithmeticException e) {

         // Handling the exception
         System.out.println(
            "Error: Division by zero not allowed");
      } finally {
         System.out.println("Program continue after exception.");
      }
   }
}

Following is the exception of the above code:

ERROR!
Error: Division by zero not allowed
Program continue after exception.

There are key differences in Exception Handling in C++ vs Java

Exception handling in java Exception handling in C++
Only throwable objects can be thrown as objects. All types can be thrown as exception
In java, finally is a block that is executed after try catch block for cleaning up. In C++ there is no existence of finally block
A new keyword throws is used to list exceptions thrown by a function. Throw keyword is used to list exceptions thrown by a function.
Both checked and unchecked exceptions are present. Only unchecked exceptions are present.
Updated on: 2025-05-30T18:40:08+05:30

645 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements