Exceptions in Java

Exception is a run-time error which arises during the execution of java program. The term exception in java stands for an exceptional event.

Exceptions in Java

Exception is a run-time error which arises during the execution of java program. The term exception in java stands for an exceptional event.

Exceptions in Java

Exceptions in Java

     

Introduction

Exception is a run-time error which arises during the execution of  java program. The  term exception in java stands  for an exceptional event. It can be defined as abnormal event that  arises during the execution and  normal flow of  program. The exceptional event can also be error in the program. 

Error in Java are of two type-

1.Compile-time Error-Compile -time Error are those error which occurs when you don't follow the Syntax of the code or do wrong in writing a code of the Programming language. The Compiler detect the Syntax  error in the code of the Programming language during its execution. For example in normal java coding ,you need your every statement to be terminated with semicolon(;).if you don't follow the rule, this will give you Compile-time error.

2.Run -time Error-Run-time error are the error which arises during the execution of a program. For example. The program runs out of the memory, result in Run-time error.

Why  Exceptions Occur-

An Exception is Run-time error that arises during the normal execution of Java program. In case of Run-time error, if you divide a number by zero or unable to open a file which does not exist, an exception is raised. In java exception are handled by Run-time System or user-defined code. A run-time error throws an exception.

Error handling plays a important role while developing an application. Following are the situation in which run-time error arises-

  1. Search a file which does not exist.
  2. Dividing a number by zero.
  3. Allocating Memory error.
  4. Problem in accessing Network Connectivity.

What is Exception Class-

Object Class is the base class of the exception hierarchy.. Object class is the super class of Throwable class.Throwable class is the super class of all the exceptional class and error class. In java you can throw exception Object which are derived from throwable class. The following Syntax for declaring a throwable class .

Throwable()

The Syntax represent a constructor of throwable class with no arguments.

In the same way we can declare a constructor of Throwable class with a userdefined Parameter.

Throwable(String Parameter)

Parameter can be message that may be userdefined included in Throwable class.

Exceptional Class-

  1. ClassNot Found Exception- Exception arises when a class is being reffered,but no such definition of class name is being found.
  2. Runtime Exception- handles the Exception arises during the runtime execution of the program.
  3. Illegal Access Exception-This Exception is thrown when method is not found.

Exceptional Hierarchy

Exception Hierarchy

Built in Exception-

The built-in exception in java are of following types classified on the basis of exception handled by the java compiler.

1.Checked Exception-These exception are the objects of the Exception class and its subclasses. It don't include the Run time Exception. These exception occurs due to invalid user input, problem in the network connectivity database connection). This also arises when we forget to import the packages. For Example java.sql.sqlexception,java.io.ioexception.The Exception problem can be overcome by implementing Try-Catch block which u see in Example in exception.

2.Unchecked Exception-These exception are the run-time errors that arises due to incorrect arguments passed to the public method written by the programmer. The Compiler never checks the Unchecked exception during the program compilation. For example, Dividing any number by zero is an unchecked exception.