- Exceptions can be primitive types or objects in C++, e.g.,
try
{
if( divisor == 0 )
throw divisor;
} catch( int exception )
{
}
In Java, exceptions are always objects of Throwable class
- C++ has catch all:
try
{
...
} catch( ... ) // catch all - default catch
{
}
The equivalent in Java is to catch an object of Exception class
- Java provides finally block that is always executed after try-catch.
C++ does not.
try
{
...
}catch( Exception exception ) // Equivalent to catch all
{
...
}
finally
{
// House-keeping / cleanup - block always executed
}
- In C++, throw is used to throw as well as pass along an exception
from one function to another.
In Java, the keyword used by a function to pass along an exception is throws
- In C++, all exceptions are unchecked.
In Java, exceptions can be checked or unchecked:
- Checked exceptions must be explicitly caught. They extend Exception class.
- Unchecked exceptions do not have to be caught. They extend RuntimeException class.