Several keywords about Java Exception Handling: Try catch/throw/throws

Source: Internet
Author: User

Http://hi.baidu.com/woaini5730/blog/item/eda79509511cdcc83bc763a7.html

 

 

 

Throws is used to declare all the exception information that a method may throw.
Throw refers to a specific exception type thrown.
Generally, throws is used to declare the exception information that a method (class) may throw at the declaration of a method (class), and throw is used to declare a specific exception information within the method (class.
Throws usually do not display caught exceptions. The system can automatically throw all caught exceptions to the upper-level methods;
Throw requires the user to capture the relevant exceptions, and then encapsulate them, and finally throw the encapsulated exception information.
Different Exception Handling Methods. Throws does not handle exceptions. The value range of throws exception is greater than the maximum range of internal exceptions of the method, and the range of cathch is greater than the range of throws exception; throw actively throws custom exception class objects. throws throw a class and throw an object.

In the definition of a method, it indicates the declarative tone, the singular number of the third party, and S is obviously required for throw. (Throws are generally used as a sub-statement for method definition)
In the function body, throw is used. In fact, it is an imperative sentence + emphasis. It is equivalent to do throw..., do + Verb prototype.

Throw is used to cause exceptions. It can cause pre-defined exceptions and custom exceptions.

I) the difference between "throws" and "throw" in an exception:

Throw is a "verb" that follows the try statement block.
Throws is a "term" used after the function method name function a () throws exception E {}
Throw is used in the program to explicitly indicate that an exception is thrown here. Throws is used in the method declaration, indicating that this method may throw an exception.
Throw is used to throw an actual exception, followed by an exception object (Instance). It is an actual statement.
Throws are used for declaration. After the method declaration, the name of an exception class is followed to indicate a general action rather than an action.

Throws is used to indicate that the current function throws an exception.
In general, some functions you call throw some exceptions. However, if you do not want to process it in the current context, you can declare that the function will throw this exception so that you do not need to try-catch it. When this exception occurs, the function will throw this exception and let the previous function handle it. Throws is also called an exception specification.
Public static H () throws
{
Try
{
A. G ();
}
Catch (exception E)
{
Throw E;
}
}
Because you did not handle this exception, but simply throws it.
Simple statement
Public static H () throws
{
A. G ();
}
This exception will also be thrown.
Ii) Try catch/throws/Throw

If a corresponding error occurs in throws, the following statements will not be executed;
The try catch comprehension should be dialectical: If the catch does not throw again, it will continue to be executed.
Throws throws an exception and cannot solve it. If you encounter a handler that can solve the exception, it is like your boss asking you to execute a task. You don't know how to solve the problem in the middle, you return the problem to your boss and think that since it is a task assigned by T, you should know how to solve the problem. Your boss cannot solve the problem and send it to the Manager for resolution, until someone can solve this problem (I don't want to write additional code to handle exceptions)
Try catch is to take into account that try may encounter this exception when containing this code, and use catch to capture and process it directly. The Code contained in catch is to process the code.

Throws throws an exception. If your upper-Layer Code has a processing method, the upper-Layer Code will handle the exception.
While the try/catch pair clearly knows what exceptions may occur in the operation, and there should be a way to handle the Catch Block.
Another method is try/catch/finaly.

Throws returns an exception to the caller, which is handled by the caller. The caller still needs to try/catch the exception and cannot run it.
Catch is a simple system. Out. println (............); In addition, the connection to the database will fail, and you do not know whether it is a driver problem, a computer problem, or a network problem. If it is sent to the user, the user cannot understand it, so throws all give catch, prompt to contact the administrator ...... This is much simpler.

After writing throws in the method signature,
Throw is written in the method body and can be written in if ()....
You can also catch an exception and immediately throw it out again. You can also catch it and throw it to create a custom exception ....

Throws throws an exception, but in the future there will be no catch to accept, or it will be thrown to the main function. It is to throw the exception step by step until one accepts the exception.

Throws throws an exception and sends it to the method that calls this method for processing. That is:
Public class test {
Public static void main (string [] ARGs ){
Test2 Test2 = new Test2 ();
Try {
System. Out. println ("invoke the method begin! ");
Test2.method ();
System. Out. println ("invoke the method end! ");
} Catch (exception e ){
System. Out. println ("catch exception! ");
}
}
}

Class Test2 {
Public void method () throws exception {
System. Out. println ("method begin! ");
Int A = 10;
Int B = 0;
Int c = A/B;
System. Out. println ("method end! ");
}
}

Obviously, the answer is:
Invoke the method begin!
Method begin!
Catch exception!

The finally statement is optional. At least one catch or one finally is required after the try statement. The finally statement provides a uniform exit for exception handling, regardless of whether an exception event occurs in the try code block, all statements in the Finally block are executed.
An exception is declared in the override method.
In a subclass, if you want to override a method of the parent class, or the method in the parent class declares a throws exception, the subclass method can also throw an exception, but remember that the exception thrown by the subclass method can only be the same type or subclass of the exception thrown by the parent class method.
For example:
Import java. Io .*;
Class {
Public void methoda () throws ioexception {
.....
}
}
Class B1 extends {
Public void methoda () throws filenotfoundexception {
....}
}
Class B2 extends {
Public void methoda () throws exception {// Error
....}
}

Public void method () throws exception {
Try {
Procedure
} Catch (exception ex ){

}
}
If a specific program fails, it will process the catch in the following program body. Throws exception is meaningless at this time.
Public void method () throws exception {
Try {
Procedure
} Catch (filenotfoundexception ex ){

}
}

If a program fails and filenotfoundexception is used, the catch in the following program body will be processed.
In this case, exceptions other than filenotfoundexception will pass throws exception and throw to the upper layer.

The throw is written in the method body, and the throws are written behind the method name.
Format of the throw Keyword: Throw new arithmeticexception (); throws an exception that can cause unchecked exception (runtimeexception) or checked execption. throw must have a try/catch statement to catch the exception.
Format of the throws keyword
Private void arraymethod (INT [] ARR)
Throws arrayindexoutofboundsexception,
Arithmeticexception {
// Body
}
The throws clause lists possible types of exceptions thrown by methods. Except for errors and runtimeexception, exceptions that may be thrown by methods must be declared in the throws list; otherwise, compilation errors may occur.
For example, if the method may throw illegalaccessexception (which belongs to checked execption), it must be declared in the throws list.

System exceptions are thrown by default. Custom exceptions must be displayed and thrown.

There are some other methods that only throw is not processed, so on the surface, you can catch exceptions without throw.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.