Saturday, March 12, 2011

Java's catching or throwing exception


The java keywords for handling exceptions are below.

  • try

  • catch

  • finally

  • throw

  • throws

Here, try,catch and finally helps us in handling the exceptions. Similarly throw and throws helps us in throwing the exception.

As stated previously, Java requires that a method either catch or specify all checked exceptions that can be thrown within the scope of the method. This requirement has several components that need further description: "catch", "specify," "checked exceptions," and "exceptions that can be thrown within the scope of the method."

Catch

A method can catch an exception by providing an exception handler for that type of exception. The next page, Dealing with Exceptions, introduces an example program, talks about catching exceptions, and shows you how to write an exception handler for the example program.

Specify

If a method chooses not to catch an exception, the method must specify that it can throw that exception. Why did the Java designers make this requirement? Because any exception that can be thrown by a method is really part of the method's public programming interface: callers of a method must know about the exceptions that a method can throw in order to intelligently and consciously decide what to do about those exceptions. Thus, in the method signature you specify the exceptions that the method can throw.The next page, Dealing with Exceptions, talks about specifying exceptions that a method throws and shows you how to do it.

No comments:

Post a Comment

Chitika