In Java, both errors and exceptions are types of throwable objects that can be raised during program execution. However, there are some key differences between them that developers should be aware of.
An error is a type of throwable object that represents a serious problem that a program is unlikely to recover from. Errors are typically caused by problems with the Java Virtual Machine (JVM) or the system environment, rather than issues with the program's logic or data. Examples of errors include OutOfMemoryError, StackOverflowError, and VirtualMachineError. Errors are not meant to be caught or handled by the program, but rather are used to inform the programmer or system administrator that something is seriously wrong with the system.
On the other hand, an exception is a type of throwable object that represents a problem that can occur during normal program execution. Exceptions are often caused by errors in the program's logic or data, such as trying to access an invalid array index or dividing by zero. Exceptions are meant to be caught and handled by the program, either by providing an alternative path of execution or by notifying the user of the problem. Examples of exceptions include NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException.
In summary, errors are meant to indicate serious system-level problems, while exceptions are meant to indicate recoverable program-level problems.
Errors V/s Exceptions In Java
Summary of Differences
Errors | Exceptions |
---|---|
Recovering from Error is not possible. | We can recover from exceptions by either using try-catch block or throwing exceptions back to caller. |
All errors in java are unchecked type. | Exceptions include both checked as well as unchecked type. |
Errors are mostly caused by the environment in which program is running. | Program itself is responsible for causing exceptions. |
Errors occur at runtime and not known to the compiler. | All exceptions occurs at runtime but checked exceptions are known to compiler while unchecked are not. |
They are defined in java.lang.Error package. | They are defined in java.lang.Exception package |
Examples : java.lang.StackOverflowError, java.lang.OutOfMemoryError | Examples : Checked Exceptions : SQLException, IOException Unchecked Exceptions : ArrayIndexOutOfBoundException, NullPointerException, ArithmeticException. |
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.