notes
  • computer-networking
    • extend-wifi-with-router
    • how-the-internet-works
    • idk
    • networking-devices
    • osi-model
    • tcp-ip
    • Types of VPN
  • databases
    • Foreign Keys
    • Redis
    • simple-queries
  • devops
    • ansible
    • Manual deployment
    • docker
    • Workflow file
    • nginx
    • promethues-grafana
    • terraform
  • hardware
    • Power
  • home-server
    • proxmox-basics
    • proxmox-setup
    • storage
  • languages-frameworks
    • programming-paradigms
    • programming-languages
      • Regex Notes
      • c
        • basics
        • pointers-memory
      • cpp
        • basics
        • running-cpp
      • php
        • basics
        • choizez
        • frameworks
          • laravel
      • python
        • venv
        • concepts
          • Using lambda
        • frameworks
          • django
            • django
            • start
      • java
        • advanced
          • functional-programming
          • reactive-programming
        • concepts
          • how-java-works
          • serialization
          • sockets
          • threads
        • extra
          • collection-framework
          • generics-and-wildcards
          • Regular Expressions (Regex)
          • streams
        • frameworks
          • orm
        • fundamentals
          • OOP
          • conditionals
          • data-structures
          • data-types
          • exceptions
          • files
          • Functions (aka method)
          • Loops
          • packages
          • type-casting
      • javascript
        • frameworks
          • morgan
          • Using Sequelize with PostgreSQL in JavaScript
  • operating-system
    • basics
    • linux-directories
    • Basic Unix Terminal Commands
  • others
    • dark-web
    • piracy
  • system-design
    • system-design
  • web-dev
    • full-stack
  • books
    • sicp
      • Recursion thought process
      • 1
        • 1.1
        • 1.2
        • 1.3
      • 2
        • 2.1
  • certifications
    • aws-certified-cloud-practitioner
      • core-services
      • other-services
    • comptia-a+
      • Cloud
      • hardware
      • Important terms
      • Important terms
      • Troubleshooting
  • cloud
    • aws
      • aws-cli
      • aws-ec2-deployment
  • dsa
    • algorithms
      • back-tracking
      • bfs
      • Binary Search
      • bit-manipulation
      • Bubble sort
      • bucket-sort
      • counting-sort
      • dfs
      • Divide & Conquer
      • djikstras-algorithm
      • dynamic-programming
      • external-sorting
      • greedy-algorithm
      • Heap sort
      • Insertion sort
      • kadanes-algorithm
      • Merge sort
      • Permutation
      • quick-sort
      • Radix Sort
      • recurrence-relation
      • recursion
      • Selection sort
      • sliding-window
      • subset
      • time-space-complexity
      • topological-sort
      • tree-traversals
      • Two Pointers Technique
    • data-structures
      • data-structures
  • security
    • authentication
      • What is JWT (JSON Web Token)?
    • software-architecture-design
      • design-patterns
Powered by GitBook
On this page
  • Error vs Exception
  • Checked vs Unchecked exception
  • Checked exception
  • Unchecked exception
  • Java Exception Hierarchy
  • Handling Exceptions
  • try , catch , finally
  • throw
  • throws
  • Custom Exception
  1. languages-frameworks
  2. programming-languages
  3. java
  4. fundamentals

exceptions

All exceptions in Java are derived from the Throwable class, which has two main subclasses:

  • Error : severe and typically unrecoverable errors (e.g. OutOfMemoryError)

  • Exception : exceptions that are recoverable and can be caught and handled.

Error vs Exception

Error
Exception

Unchecked exception

Checked & Unchecked exception

Caused by environment of application

Caused by application

During run-time

During compile time or run-time

Checked vs Unchecked exception

Checked exception

aka. compile time exception

must be either caught (try-catch) or declared in the method signature using the throws keyword.

Unchecked exception

aka. run time exception

Do not need to be explicitly caught or declared. They typically indicate programming error that should be addressed

Java Exception Hierarchy

Handling Exceptions

Note , can use generic "Exception" class since all exceptions extend from it or use a specific exception

try , catch , finally

try {
    // Code that may throw exceptions
} catch (ExceptionType1 e1) {
    // Handle ExceptionType1 , can log out the exception
} catch (ExceptionType2 e2) {
    // Handle ExceptionType2
} finally {
    // Optional: Code that always runs, regardless of whether an exception occurred (can use for cleanup)
}



//Multiple exception shorthand
try {
    // Code that may throw exceptions
} catch (NullPointerException | NumberFormatException e) {
    // Handle both NullPointerException and NumberFormatException here
}

throw

Explicitly throw an exception

if (errorCondition) {
    throw new CustomException("This is a custom exception message");
}

throws

public void someMethod() throws IOException {
    // Method code that may throw an IOException
}

Custom Exception

Can add custom functionalities for custom exception

public class CustomException extends Exception {
    private int errorCode; // Additional error code
    private String errorDetails; // Additional error details

    public CustomException(String message) {
        super(message);
    }

    public CustomException(String message, int errorCode, String errorDetails) {
        super(message);
        this.errorCode = errorCode;
        this.errorDetails = errorDetails;
    }
}
Previousdata-typesNextfiles

Last updated 1 month ago

Java Exception Hierarchy