Youtube Channel

Exceptions

Exception Handling
In real industry to develop any type of application we need to certain programming language. In any programming language we can come across three types of errors they are-
 Compile time errors
Compile time errors are those which are resulted at compile time because syntax's are not followed by programmers
As a java programmer we solve the compile time error at development or production level.

Logical errors

Logical errors are those which are occurring at execution or runtime because of mis-interpretation of logic by the programmers.
Logical error always generates inconsistent results. Logical errors are solved by programmers at development level.

Run time errors

These are those which are occurring at execution/runtime.
Runtime errors are basically occurring at implementation level because application users are entering wrong or invalid input to the programs.
The programming languages like c,cpp,pascal etc and whose applications are comes under weak because these languages are not containing a facility called execution handling.
The languages like java and .net are comes under strong because these languages are comes under strong because these languages are containing a facility called exception handling.
In real world if any application user enters invalid input to any project then by default the project generates system/technical error message which are understandable by programmers but not by application users.
Industry is highly recommended to generate user friendly error message instead of generating default system error message.
User friendly error message always makes the application user to understand what type of mistake is committed during entering the input.
Hence, it is highly recommended to every programmer to build strong application by making use of exception handling concepts.
Points to remember in exception handling
  1. Whenever the application user enters invalid input to any program then it will generate run time error (run time error occurs due to invalid input).
  2. Run time errors of any program by default generates system error message
  3. Exception: run time error of java program is known as exception.
  4. Exception always by default system error message.
  5. Whenever an exception occurs in the java program , program execution is abnormally terminated cpu control comes out of program flow and jvm generate system error message but it recommends to generate user friendly error message by making use of exception handling.
  6. Definition of exception handling: the process of converting system error message into user friendly error message.
System error message
User friendly message
Exception handling
 Each and every operation /task/action must be performed in exception handling with respect to object.
We know that whenever an exception occurs in the java program. Program execution is abnormally terminated cpu control comes out of program flow and jvm generates system/technical error message.
Generating system error message is an action or operation and it must be performed with respect to object. Object creation requires a class and this class is one of the exception classes. This exception class object is automatically created by jvm and generates system error message which are not recommended in the industry. The same exception class is used by java programmer and generates user friendly error message.
If the exception class is developed by the java programmer then it is known as user or programmer defined exception .if the exception class is developed by sun micro system developers then they are called predefined exception.
Types of exception in java
  1. Predefined /built in exception.
  2. User/Programmer defined exception.
Predefined exceptions are those developed by sun micro system developers and supplied as a part of java software and whose role is to deal with universal problems.
Some of the universal problems are
  • division by zero problem
  • invalid number format
  • Invalid bounds of arrays etc.
User defined exception are those which are developed by java programmers and supplied as a part of their project and whose role is to deal with common problems.
Some of the common problems are
  • Trying to deposit invalid amount in the balance of the account.
  • Trying to enter invalid user name and password.
  • Trying to enter negative age to human being.
  • Trying to enter invalid no of password characteristics.

Types of predefined exceptions

  1. Asynchronous Exception
  2. Synchronous Exception

Asynchronous exception

These are an exception which always deals with hardware problems and external problems like motherboard, keyboard, mouse, power failure etc.
In real industry as on today we are not using asynchronous exception because sun micro system developers didn’t develop complete API for developing asynchronous application   but it is in research and development of Oracle Corporation. To deal with all asynchronous exception we have a predefined super class called java.lang.Error.
Java .lang.Error
 e.g StackOverFlowError

 Synchronous exception

This exception deals with programmatic run time errors. Synchronous exception are classified into two types they are
  1. Checked exception
  2. Unchecked exception

Checked exception

These are those which are direct sub classes of java.lang.Exception or exception which are occurring at run time will be showing them as errors at compile time.
Java.lang.Exception
  • ClassNotFoundException
  • FileNotFoundException
  • IOException
  • IntrruptedException

Unchecked Exception

Are those which are direct sub sub class of java.lang.RunTimeException
java.lang.RunTimeException
  • ArithmeticException
  • NumberFormatException
  • ArrayIndexOutOfBoundException
 Note: Even though java.lang.RunTimeException is sub class ofjava.lang.Exception should not be considered as checked exception.

Exception handling Hierarchy

The following diagram gives hierarchy of classes involving in exception handling hierarchy.

0 comments: