Sitemap
Dinushkas

Dinushkas

Exceptions in JAVA

2 min readFeb 24, 2022
  • What is “ Error “ & “ Exception“?

Let me briefly explain what's “Error” and what's “Exception” first .Here's little Fun-fact about error and exception :D

-> Both error and exceptions are sub-classes of Throwable class.

Exceptions is problem that occurs during the execution of the program. Here some reasons why exceptions occurs .

  1. A user may have input invalid data.
  2. JVM maybe runout of memory.
  3. File not found.

Exceptions need to be handled otherwise Exceptions might lead to system failure. Exceptions can be occur during run-time or compile-time. Exceptions either can be Checked or Unchecked.

Get Dinushkas’s stories in your inbox

Join Medium for free to get updates from this writer.

Check out this below code snippet :

so after compiling the above code I get below output.This occurs because im trying to access a 10th index of the size of an array three.

Press enter or click to view image in full size

In here ill use a try-catch block to avoid the above situation.

Press enter or click to view image in full size

try block is the block of code where exception occur. It allows you to define a block of code to be tested for errors while it is being executed.

catch block it allows you to define a block of code to be executed, if an error occurs in the try block.

Error occurs in the run-time and it occurs when illegal operation operation performed by the user and it may cause abnormal working of the program .

--

--