Skip to main content

Command Palette

Search for a command to run...

Java Exception Handling

Updated
1 min readView as Markdown
Java Exception Handling
A

Undergraduate student 😊🤞

An exception interrupts a computer program's normal flow. In Java programming language, an exception object is anything that aims at representing an error or abnormal event during program execution. Exception handling is a procedure that developers use to catch such errors and let the program continue running without crashing.

Types of Exceptions

1)Checked Exceptions: using at compile time. These are handling by try catch block.by declaring them with the "throws" keyword. Example: IOException.

2)Unchecked Exceptions: usually result from programming errors. Example: NullPointerException.

3)Errors: grave issues that your program is not designed to detect. Example: OutOfMemoryError.

Handling Exceptions with Try-Catch

  • try block: Code that might throw an exception.

    catch block: Code that handles the exception.

The Finally Block

Whether an exception was thrown or not, code that always executes is contained in the finally block.

Throwing Exceptions

You can also throw exceptions manually using the "throw" keyword.

Custom Exceptions

By extending the Exception class, you can make your own exceptions.

It is essential to comprehend exception handling when creating dependable Java programs. Finally, use try-catch blocks for cleanup and, if needed, build custom exceptions. If you keep at it, managing exceptions will come naturally to you. Have fun with coding!