Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

26. Exceptions in Java

 

Exceptions in Java

  • An exception is an unwanted or unexpected event that arises during the execution of a program.
  • The normal flow of the program is disrupted and the program terminates abnormally, which is not recommended.
  • An exception can occur for many different reasons. Some of the following scenarios where an exception occurs are:
    • A user has entered an invalid data (eg. 100/0).
    • A file that needs to be opened cannot be found.
    • A network connection has been lost in the middle of communications.

Types of Exceptions

There are mainly two types of exceptions in Java:

Checked Exceptions

  • These exceptions are checked by the compiler at compilation-time.
  • So these are also called as compile-time exceptions.
  • These exceptions cannot be simply ignored by the programmer.
  • The programmer should handle these exceptions.
  • Example: IOException, SQLException, ClassNotFoundException etc.

Unchecked Exceptions

  • These exceptions occurs at the time of execution.
  • So these are also called as run-time exceptions.
  • These exceptions are ignored at the time of compilation.
  • These includes programming bugs, such as logic errors or improper use of an API.
  • Example: ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

Errors v/s Exceptions

  • An error indicates serious problem that a reasonable application should not try to catch.
  • Exception indicates conditions that a reasonable application might try to catch.
  • When an error occurs, either the program will not successfully compile or the program will execute completely but not successfully.
  • When an exception occurs, the program will successfully compile but will not execute completely and successfully.

Exception Hierarchy

  • All exceptions and errors are sub classes of class Throwable, which is base class of hierarchy.
  • Exception class is used for exceptional conditions that user programs should catch.
  • Error class is used by the Java run-time system (JVM) to indicate errors having to do with the run-time environment itself(JRE).

Previous

Next

Comments