Java exception Series 1: About throws and throw

Source: Internet
Author: User

Throws
Exception description, indicating a methodPossible
An exception is thrown. After a method is used, it is followed by a list of potential exceptions, such as void func () throws.

Aexception, bexception {//....
1). If there is a method void a () throws aexception {}, this method will not throw an exception generated by aexception or its subclass, because even if it is displayed
Throws with the keyword throws. If method main () calls a (), either capture (snippet1.1) or continue throws in method main ().
(Snippet1.2 ).

// Snippet1.1
Public class throwsexceptioncatch {

Public static void main (string [] ARGs ){
Try {
A ();
} Catch (sqlexception SE ){
System. Out. println ("dispose sqlexception .");
}
}

Static void a () throws sqlexception {
System. Out. println ("throws sqlexception .");
}

}

// Snippet1.2
Public class throwsexception {

Public static void main (string [] ARGs) throws sqlexception {
A (); // not catch due to above "throws sqlexception"
}

Static void a () throws sqlexception {
System. Out. println ("throws sqlexception .");
}

}

2). For void a () {}, regardless of whether the () method may generate an aexception, throws aexception can be declared after the method, similar to Servlet
The doget/dopost method, whether or not servletexception is generated in the doget/dopost method, in eclipse, the ServletCodeDefault template generation
Throws servletexception. Why do we need to do this? I personally feel that this design pattern is related and is conducive to coupling. It is worth noting that no matter method
() Whether an aexception is generated internally. Once throws aexception is declared, the caller of method A () can either capture or throw it. snippet1.1 and 1.2 have already said
Ming.

Throw
, Indicates that an exception is generated and can only be used inside the method. Its usage is "Throw exception object", for example, void func () {Throw new aexception ();}. Or
It can be used together with the exception declaration. For throw, catch is required for some exceptions. Otherwise, compilation errors may occur. For details, seeJava exception Series 2

.

Major differences between throws and throw:
One usage: the former A () throws aexception, bexception {//...; the latter a () {Throw new aexception ();}.
The former indicates that an exception may be thrown; the latter indicates that an exception is generated.
3. The former A () is called by B (), and B () is either caught or thrown. Some exceptions generated by the latter must be captured within, some do not need to be used.

 

If it is reprinted, please explain the source! Http://blog.csdn.net/xukunddp

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.