
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between Exception and Error in Java
Exceptions and errors both are subclasses of Throwable class. The error indicates a problem that mainly occurs due to the lack of system resources and our application should not catch these types of problems. Some of the examples of errors are system crash error and out of memory error. Errors mostly occur at runtime that's they belong to an unchecked type.
Exceptions are the problems which can occur at runtime and compile time. It mainly occurs in the code written by the developers. Exceptions are divided into two categories such as checked exceptions and unchecked exceptions.
Sr. No. | Key | Error | Exception |
---|---|---|---|
1 | Type | Classified as an unchecked type | Classified as checked and unchecked |
2 | Package | It belongs to java.lang.error | It belongs to java.lang.Exception |
3 | Recoverable/ Irrecoverable | It is irrecoverable | It is recoverable |
4 | It can't be occur at compile time | It can occur at run time compile time both | |
5 | Example | OutOfMemoryError ,IOError | NullPointerException , SqlException |
Example of Error
public class ErrorExample { public static void main(String[] args){ recursiveMethod(10) } public static void recursiveMethod(int i){ while(i!=0){ i=i+1; recursiveMethod(i); } } }
Output
Exception in thread "main" java.lang.StackOverflowError at ErrorExample.ErrorExample(Main.java:42)
Example of Exception
public class ExceptionExample { public static void main(String[] args){ int x = 100; int y = 0; int z = x / y; } }
Output
java.lang.ArithmeticException: / by zero at ExceptionExample.main(ExceptionExample.java:7)
- Related Articles
- Difference Between Error and Exception in Java
- Differentiate between exception and error in PHP
- Difference Between Checked and Unchecked Exception in Java
- Difference between Interrupt and Exception
- What are the differences between an Exception class and an Error class in Java?\n
- Difference between system level exception and Application level exception.
- What is the difference between throw e and throw new Exception(e) in catch block in java?
- What is the difference between error () and ajaxError() events in jQuery?
- What is the difference between Flow Control and Error Control?
- What is the difference between 'except Exception as e' and 'except Exception, e' in Python?
- How to print the Python Exception/Error Hierarchy?
- Difference between Java and JavaScript.
- Difference between Go and Java.
- Difference Between C++ and Java
- Difference between Groovy and Java

Advertisements