Both languages use try, catch and throw keywords for exception handling, and meaning of try, catch and free blocks is also same in both languages. Following are the differences between Java and C++ exception handling.
1) In C++, all types (including primitive and pointer) can be thrown as exception. But in Java only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be thrown as exception. For example, following type of code works in C++, but similar code doesn’t work in Java.
Exception occurred: thrown value is -1
2) In C++, there is a special catch called “catch all” that can catch all kind of exceptions.
Exception occurred: exiting
In Java, for all practical purposes, we can catch Exception object to catch all kind of exceptions. Because, normally we do not catch Throwable(s) other than Exception(s) (which are Errors)
3) In Java, there is a block called finally that is always executed after the try-catch block. This block can be used to do cleanup work. There is no such block in C++.
Got the Test Exception Inside finally block
4) In C++, all exceptions are unchecked. In Java, there are two types of exceptions – checked and unchecked. See this for more details on checked vs Unchecked exceptions.
5) In Java, a new keyword throws is used to list exceptions that can be thrown by a function. In C++, there is no throws keyword, the same keyword throw is used for this purpose also.
6) In C++ if the exception isn’t caught then the exception handling subsystem calls the function unexpected(), which terminates the program or an application abnormally. If any exception arise in our C++ program then finding that particular exception is very time consuming because in C++ unexpected() did not tell us that which type and on which line the exception has occurred. For more details on unexpected() refer this.
But in Java if the system generated exception isn’t caught then the java runtime system(JVM) handover the exception object to the default exception handler, which basically prints the name,description and on which line the exception has occurred. So in Java finding and handling the exception is easier than the C++ language. For more details on default exception handler refer this.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Output of Java program | Set 12(Exception Handling)
- Nested try blocks in Exception Handling in Java
- Exception Handling with Method Overriding in Java
- Version Enhancements in Exception Handling introduced in Java SE 7
- Exception handling and object destruction | Set 1
- Exception handling in JSP
- Handling the Divide by Zero Exception in C++
- Exception Handling in C++
- Comparison of static keyword in C++ and Java
- Comparison of Inheritance in C++ and Java
- Comparison of boolean data type in C++ and Java
- Comparison of double and float primitive types in Java
- Comparison of yield(), join() and sleep() in Java
- Java 11 - Features and Comparison
- Comparison of Autoboxed Integer objects in Java
- Image Processing in Java | Set 14 ( Comparison of two images )
- Comparison of Java with other programming languages
- Brief Overview & Comparison of Object-Oriented Programming from C to Java
- User-defined Custom Exception in Java
- Infinity or Exception in Java when divide by 0?