Lets Start With Exception

Snehatiwari
3 min readJun 9, 2021
  • Exception: Exception is an abnormal condition.An exception is an event that distrupts the normal flow of program. The reason behind the exception may be lack of knowledge or coding and etc. can solve or handle the exception.It occurs at the run time.
  • Error: Errors indicates the serious problem that can not be handled. some of the problem like out of memory error,system crash error
    and etc.

What is Exception handling?

Exception handling in java is one the powerful mechanism to handle the runtime exception so that the normal flow of the application can be maintained. runtime exception like: ClassNotFoundException,IOException,SQLException,etc

Exception hierarchy

Exception Handling Keywords

  • Try : it is used to specify a block of code where we suspect the exception occur. Try cannot be a standalone block. Hence ther must be a catch or finally block.
  • Catch : it is used to define block where we handle the exception.It is only executed when an exception occurs and is handled.We cannot use catch as standalone.
  • Finally : it is used to define a block which includes all the important codes of the program that we want to execute,whether an exception is handled or not.
  • Throw : System generated exceptions are automatically thrown by the java run time system. Now if we want to manually throw an exception,we have to use the throw keyword.
  • Throws : The throws keyword is used to create user defined exception handling in java. It is used to declare a custom exception. This keyword shows the chances of an exception occuring in a program

Example

In this example you will see the exception

This exception occured because we are trying to divide a number by zero. So it gave exception java.lang.ArithmeticException.Due to this exception rest of code didn’t execute.

Now we will use these exception handling keywords to handle the exception so that our code run.

In catch block always we will have to give right exception name .

the output of above code you will see below:

Some exceptions name with meaning:

I hope this blog will be helpful for you to understand about exception. Try to solve all exception.

Thankyou

Sneha tiwari

--

--