IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Exception Handling Fundamentals

Go down

Exception Handling Fundamentals Empty Exception Handling Fundamentals

Post by BIT0112-Rokon Thu Oct 29, 2009 3:39 am

Exception Handling Fundamentals


Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. They form an interrelated subsystem in which the use of one implies the use of another. However, it is useful at the outset to have a general understanding of the role each plays in exception handling. Briefly, here is how they work. Program statements that you want to monitor for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception using catch and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw. In some cases, an exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed upon exiting from a try block is put in a finally block.


Q: Just to be sure, could you review the conditions that cause an exception to be generated?

A: Exceptions are generated in three different ways. First, the Java virtual machine can generate an exception in response to some internal error which is beyond your control. Normally, your program won’t handle these types of exceptions. Second, standard exceptions, such as those corresponding to divide-by-zero or array index out-of-bounds, are generated by errors in program code. You need to handle these exceptions. Third, you can manually generate an exception by using the throw statement. No matter how an exception is generated, it is handled in the same way.
 


Using try and catch

At the core of exception handling are try and catch. These keywords work together; you can’t have a try without a catch, or a catch without a try  Here is the general form of the try/catch exception handling blocks:

Code:

try {
    // block of code to monitor for errors
     } catch (ExcepType1 exOb) {
            // handler for ExcepType1    

      }catch (ExcepType2 exOb) {                  
       // handler for ExcepType2      
      }
.
.
.


Here, ExcepType is the type of exception that has occurred. When an exception is thrown, it iscaught by its corresponding catch statement, which then processes the exception. As the general form shows, there can be more than one catch statement associated with a try. The type of the exception determines which catch statement is executed. That is, if the exception type specified by a catch statement matches that of the exception, then that catch statement is executed (and all others are bypassed). When an exception is caught, exOb will receive its value. Here is an important point: If no exception is thrown, then a try block ends normally, and all of its catch statements are bypassed. Execution resumes with the first statement following the last catch. Thus, catch statements are executed only if an exception is thrown.


Source:
Java™️: A Beginner’s Guide,
Third Edition
Herbert Schildt
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum