Wednesday, 29 February 2012

Exception Handling in JAVA:



·        In Java, exception handling is managed via five keywords:
o    Try
o    Catch
o    Throw
o    Throws
o    finally
·        Program statements that should be monitored for exceptions are contained within a try block.
·        If an exception occurs within the try block, it is thrown. The exception which is thrown should be caught by the code (using catch) and that should also be handled.
·        System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw.
·        Any exception that is thrown out of a method must be specified as such by a throws clause.
·         Any code that absolutely must be executed before a method returns is put in a finally block.

This is the general form of an exception-handling block:

try
{
// block of code to monitor for errors
}
catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb)
{
// exception handler for ExceptionType2
}
// ...
finally
{
// block of code to be executed before try block ends
}

·        Exception Type is the type of exception that has occurred.

No comments:

Post a Comment

IE7 Issues Text Indent

                 Unfortunately IE 7 is still widespread among the users hence while theming we have to give special importance to the grea...