| Mobile | RSS

Java Tutorial : Exception Handling 

28th Sep, 2009 | 3 Comments | Posted in Java by Alex Jose

The term exception in Java indicates an exceptional event. It can be defined as an abnormal event that occurs during program execution and disrupts the normal flow of instructions. The abnormal event can also be an error in the program.

Errors in a Java program are categorized into two groups: compile-time errors and run-time errors. Compile-time errors occur when you do not follow the syntax of a programming language. The compiler detects the syntax error of the program while compiling the program. For example in Java, you need to terminate every expression with a semicolon (;). If you do not follow this rule you get a compile time error.

Run-time errors occur during the execution of a program. For example, if the program runs out of memory, it results in run-time error.

Concept of Exceptions

An exception is a run-time error that occurs during the execution of a Java program. For example, if you divide a number by zero or open a file that does not exist, an exception is raised. In Java, exceptions can be handled either by the Java run-time system or by a user-defined code. When a run-time error occurs, an exception is thrown.

Error handling becomes a necessity when you develop applications that need to take care of unexpected situations. The unexpected situations that may occur during program execution are:

  1. Running out of memory
  2. Resource allocation errors
  3. Inability to find files
  4. Problems in network connectivity
Related Posts with Thumbnails