Skip to main content

Basic Exception Handling


What is Exception:

Excetion is a runtime error caused due to logical mistake occured in the program beacause of wrong input given by the User.
  • According  to Java language, value, message or excpetion everything is an object.
  • In function programming, we return value.
  • Throwing exception is like returning value and If we see according to User,methods are just services nothing else.

What is our(developer's) responsibility:

We must convert Java Exception message into End User Understandable.Here, Conversion I mean,catching Exception object and get information using getters provided such as getMessage() and modify this message with End user understandable format.
Sun Microsystem(now Oracle) defined several classes to represent and handle logical mistakes.
All these classes are defined in java.lang package with a super class Throwable.

All Exception handling classes categorized into two sub-categories:
  1. Error
  2. Exception
Exception classes are again divided into two categories (These are not class names):
  1. Runtime Exceptions
  2. Checked Exceptions
So according to that all exception handling classes are divided into three categories:
  1. Errors
  2. Runtime Exceptions
  3. Checked Exceptions
  • All sub-classes of java.lang.Error are under Error category
  • All sub-classes of java.lang.RuntimeException are under Runtime Exception category.
  • java.lang.RuntimeException is a sub-class of java.lang.Exception class and remaining all subclasses are checked Exceptions.


So according to above diagram,Errors and Runtime Exceptions are called as unchecked exceptions

Error, RuntimeException and their subclasses are called Unchecked Exceptions because their exception handling is not checked by the compiler as they are intended to raise and throw at runtime not by Developer.
Even if they are raised by Developer Compiler will not take note of it and doesn't check their handling.

Following are some of the examles of Error which are raised only at runtime due to problem occured in JVM:
  1. StackOverflowError
  2. OutOfMemoryError
  3. NoClassDefFoundError
  4. NoSuchMethodFoundError
Following are the examles of Runtime Exceptions which are raised only at runtime due to operation execution failure:
  1. ArithmeticException
  2. ArrayIndexOutOfBoundException
  3. NullPointerException
  4. ClassCastException
Throwable, Exception and its direct sub-classes are called as checked exceptions because this exceptions handling is checked by compiler cause these exceptions are thrown explicitly by developer

Rule:

If developer is throwing checked exception then those exceptions must be reported to its method caller else compiler throws compile time error saying
"Unreported exception must be caught or declared to be thrown"

How can developer throw and report exception?


Using throw keyword, Developer can throw exception and using throws keyword can report an exception

Syntax:

class Example{
 void m1() throws <exception_class_name>{
throw <exception_class_object>;
 }
}

We use throw and throws keyword for raising or throwing  and reporting checked exception,, we can also use them for throwing unchecked exception

Rule:

throws keyword is mondatory to report checked exception, it is optional for unchecked exception.

Example:


class Example{
 void m1() throws ClassNotFoundException{
throw new ClassNotFoundException();
 }

 public static void main(String args[]){
Example example = new Examplle();
example.m1();
 }
}

Comments

Popular posts from this blog

Restful API for Beginners

Introduction: Everyone is saying Rest API. Sometimes people say  only API to rest apis.RESTful API is for accessing blah blah service or to do blah blah functionality. In corporate world, fresher get confused when seniors give them task to create new api to call blah blah service or to do blah blah functionality. So what is this REST API? REST (REpresentational State Transfer) is an architectural style, and an approach to communications.Using this REST whatever functionality we create known as REST API. We can implement  rest-api using various providers but usually people use Jersey and Spring . As per my choice personally I would like to use jersey which keep code neat and clean. Rest follows client-server architecture with Front Controller Design Pattern. It completely depends on http protocol .REST implementation is very easy and run in  less memory compared to SOAP. Rest Support following parameter techniques to pass input for our web service...

Exception Propagation and throw-throws keyword

Exception Propagation: Sending exception from one method to another method is called exception propagation Here propagation is nothing but sending. So guys what is the situation when we are going to implement this propagation concept? Answer to this question is when we throw an exception, if it is not caught in that method it is propagated to another method and interesting thing is that we don’t need to do anything in this process we just need to throw an exception that’s it. Rule: If the propagated exception is checked exception then not only current method developer but also method’s caller method developer should caught or report that checked exception else it leads to same compiler exception….CE: Unreported Exception So according to that basic rule is “If checked exception is raised the method either directly by using throw keyword or indirectly by a method call it must be caught or reported. void m1() throws ClassNotFoundException{ ...

User Defined Exception Handling

Introduction: To develop the project, we must create our own Exception class, as Sun microsystem has not given any exception class some requirements which are not in common. So, what is User defined exception is? Exception class defined by Developer is called as User Defined Exception or Custom exception Steps to create User Defined Exception: Write a class that extends java.lang.Exception. In User defined class, define minimum 2 constructors. No-argument constructor String parameter with logic “super(message)” to create exception object with message.  No need to define any method, why because exception class is not meant for developing business logic , instead it is used for terminating method logic execution by throwing it’s object. ------------------------------------------------------------------------------------------------------------ package com.app.exceptions; /**  *  * @author Shashank Mungantiwar  * ...