Exception and Error Concept Simplified in Java

0
283
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

The purpose of Exception handling is to provide a means to detect and report an “Exceptional circumstance “,so that a proper action can be taken.
The process is as follows:

Process-I

  1. Find the problem(Hit the exception)
  2. Inform that an exception/error has occurred (throw the exception)

Process-II

  1. Receive the error information(Catch the exception)
  2. Take corrective actions(Handle the exception)

Error: 

Errors are wrong/mistakes that can make a code go wrong.An error may produce an incorrect output or may terminate the execution.Errors can abruptly stop an execution or may crash the system.Errors can be divided into two catagories:

  • Compile time error
  • Runtime error

Compile time errors:

All syntax errors will be detected and displayed by the java compiler and therefore these errors are known as compile time error.Whenever compiler displays an error,it will not create the .class file.Hence it is necessary to fix all of them to successfully compile and get the .class file.
Once the compile time error occurs ,we need to go to the appropriate lines ,correct the errors and recompile the program.We should generally consider the earlier errors as the major source of our problem.After we fix such an error,we should recompile to look for other errors.
Compile time errors are mainly due to the following:
  • Typing mistake
  • Missing semicolons
  • Missing or mismatches of brackets in classes or methods
  • Misspelling of identifiers or keywords
  • Missing double quotes in String
  • Use of undeclared variables.
  • Incompatible types in assignments/initialization.
  • Bad references to objects
  • Use of ‘=’ instead of ‘==’
  • Javac: command not found.(Due to either java is not installed or path issue)

Runtime errors:

Error are referred as Runtime error and its sub classes are used for serious errors from which programs are not suppose to recover.They are not at all Exceptions. Errors are generally ignored during compilation time.
Exp-out of memory error.
If compiler displays an error it will not create a .class file.While runtime error occurs,program will stop execution after displaying error message.

Runtime errors are mainly due to the followings:

  • Dividing an integer by zero.
  • Accessing an element that is out of bounds of an array or String.
  • Trying to store a value into an array of an incompatible type or class.
  • Trying to cast an instance of a class to one of its subclases.
  • Passing a parameter that is not a valid range or value for a method.
  • Trying to illegally change the state of a thread.
  • Attempting to use  a negative size for an array.
  • Using a null object reference as a legitimate object reference to access a method or variable.
  • Converting invalid String to a number.
  • Accessing a character that is out of bounds of a String. etc


Why force Checked Exceptions thrown by a method to be caught but not Unchecked Exception?

Checked Exceptions thrown by a method are part of the public programming interface.Those who invokes the method must be aware of them and should  decide what to do with them.On the other hand, unchecked Exceptions are result of programming problem. It is usually not possible to recover from those problems.There could be numerous possibilities for Unchecked Exceptions,trying to cover all of them will reduce that code clarity.

Rule of Thumb while designing the public API that throws Exception:

  1. If the API user can take some meaningful action from the information available in the Exception object,We need to declare the Exception as checked.
  2. if the API user can not do anything meaningful after the Exception is thrown,We need to declare it as Unchecked Exception

Rule of Thumb with Exceptions Across Layers:

We need to make sure implementation of specific lower level exception should never propagate to higher levels(Low level Data access exception should not propagate to higher level like business level as it does not make any sense there)

Note:
  • A statement in a method can throw an exception that is not matched by a catch clause.This includes situations in which the statement throwing the exception is not even inside a try catch block.When this happens , execution of the program jumps out of the method that contains the faulty statements.Execution jumps back to whatever code called the method in the first place.
  • A method can name more than one exceptions type in its throws clause. We need to use comma to separate these exception types.
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

LEAVE A REPLY

Please enter your comment!
Please enter your name here