Java handle Exceptions

来源:互联网 发布:必赢客pk10软件下载 编辑:程序博客网 时间:2024/05/21 17:09

Java handle Exceptions

You usually code at least one catch block immediately following a try block. A catch block is a segment of code that can handle an exception that might be thrown by the try block that precedes it. The exception might be one that is thrown automatically, or you might explicitly write a throw statement. Aa throw statement is one that sends an Exception out of a block or a method so it can be handled elsewhere. A thrown Exception can be caught by a catch block. Each catch block can “catch” one type of exception – that is, one object that is an object of type Exception or one of its child classes.

If you do not include a catch block immediately after a try block, then you must code a finally block.

If a try block calls the System.exit() method and the finally block calls the same method, the exit() method in the finally block executes. The try block’s exit() method call is abandoned.

If a method throws an Exception that it will not catch but that will be caught by a different method, you must use the keyword throws followed by an Exception type in the method header. This practice is known as exception specification.

Java’s exceptions can be categorized into two types:

Unchecked exceptions: These exceptions inherit from the Error class or the RuntimeException class. Although you can handle these exceptions in your programs, you are not required to do so. For example, dividing by zero is a type of RuntimeException, and you are not required to handle this exception – you can simply let the program terminate.

Checked exceptions: These exceptions are the type that programmers should anticipate and from which programs should be able to recover. All exceptions that you explicitly throw and that descend from the Exception class are checked exceptions.

Java programmers say that checked exceptions are subject to the catch or specify requirement, which mean if you throw a checked exception from a method, you must do one of the following:

Catch it within the method

Specify the exception in your method header’s throws clause.

Code that uses a checked exception will not compile if the catch or specify rule is not followed.

If you write a method with a throws clause in the header, then any method that uses your method must do one of the following:

Catch and handle the possible exception.

Declare the exception in its throws clause. The called method can then rethrow the exception to yet another method that might either catch it or throw it yet again.

In other words, when an exception is a checked exception, client programmers are forced to deal with the possibility that an exception will be thrown.

If you write a method that explicitly throws a checked Exception that is not caught within the method, Java requires that you use the throws clause in the header of the method. Using the throws clause does not mean that the method will throw an Exception – everything might go smoothly. Instead, it means the method might throw an Exception. You include the throws clause in the method header so applications that use your methods are notified of the potential for an Exception.

When a method might throw more than one Exception type, you can specify a list of potential Exceptions in the method header by separating them with commas.

The memory location known as the call stack is where the computer stores the list of method locations to which the system must return.

If method() calls method(), and methodB() calls methodC(), and methodC() throws an Exception, Java first looks for a catch block in methodC(). If none exists, Java looks for the same thing in methodB(). If methodB() does not have a catch block, Java looks to methodA(). If methodA() cannot catch the Exception, it is thrown to the Java Virtual Machine, which displays a message at the command prompt.

原创粉丝点击