Difference Between Errors and Exceptions

Start course
Overview
Difficulty
Beginner
Duration
50m
Students
24
Description

In this course, we'll learn about Exception Handling in Java.

Learning Objectives

  • What is an Exception?
  • Difference between Error and Exception
  • Types of Exceptions
  • Try-Catch Block
  • Finally Block
  • Throw and Throws Keywords
  • Exception Methods

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In our previous video, we tried to explain what an exception is. In this video, we'll talk about the difference between error and exception. If you're ready, let's start. Error is a critical condition that cannot be handled by the code of the program. Exception is an exceptional situation that can be handled by the code of the program. The important difference between error and exception is that an error is caused due to a lack of system resources, but an exception is caused because of your code or program. Let's look at the other differences between error and exception along with a comparison chart. As you see in the table, in Java, errors are defined in the "java.lang.Error" package but exceptions are defined in the "java.lang.Exception" package. 

An error is caused due to a lack of system resources, an exception is caused because of the code. It is impossible to recover from errors, but you can recover from exceptions by handling them through "try-catch blocks". Errors are classified as unchecked types, exceptions are classified as checked or unchecked types. If we want to give examples of error, StackOverflowError and OutOfMemoryError. If we want to give examples of exception, ClassNotFoundException and NullPointerException. Okay, now, let's take a close look at the hierarchy of the Java exception class. As you can see in the diagram, Object is the top most class in Java. All the classes extend the object class by default in Java. It is the super class. 

Here, Throwable is the top most class of exception classes, and it's located in the java.lang package. Throwable has two subclasses, Error and Exception. Exceptions have been categorized in two ways; built-in exceptions and user-defined exceptions. All checked and unchecked exceptions are built-in exceptions of Java. Okay, let's take a short break here. In the next video, we'll learn about these exceptions in detail. See you in the next video.

 

About the Author
Students
458
Courses
47
Learning Paths
3

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics