Javatpoint Logo
Javatpoint Logo

Exception Handling in C++ and JAVA

1. Exceptions can be thrown in C++ for any type (including primitive and pointer). However, in Java, only throwable objects (instances of any subclass of the Throwable class) can be thrown as exceptions. For example, while the following code works in C++, it does not work in Java.

C++

Output

Exception occurred: thrown value is -1

2. There is a special catch in C++ called "catch all" that can catch all types of exceptions.

C++

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)

3. Finally is a block in Java that is always executed after the try-catch block. This block can be used to perform cleanup tasks. In C++, there is no such block.

JAVA

Output

Got the Test Exception
Inside finally block 

4. 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.

5. 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.

6. 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, however, if the system-generated exception is not caught, the java runtime system (JVM) passes the exception object to the default exception handler, which prints the name, description, and line number where the exception occurred. As a result, finding and handling exceptions in Java is simpler than in C++.

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

The differences between Java and C++ are listed below.

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.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA