Java-exception advanced-use of packages

Source: Internet
Author: User
Tags try catch

A finally1.1 the order of exception execution
Package test; Public classTest { Public Static voidMain (string[] args) {Demo d=NewDemo (); Try{d.show (-5); System. out. println ("Hello try"); } Catch(noshowexception e) {e.printstacktrace (); System. out. println ("Hello catch"); }    }}classNoshowexception extends exception{noshowexception (String message) {super (message); }}classdemo{voidShowintnum) throws noshowexception {if(num<0)            Throw NewNoshowexception (num+", the value is illegal."); System. out. println ("show ... .."+num); }}

Results:

1.2 Finally function

There are some specific code that needs to be executed, regardless of whether the exception occurs.
Because the exception causes the program to jump, the write statement does not execute. Unable to meet this requirement.
Java provides a solution when exception capture is processed.

application scenarios;

define a feature to add data to the database. voidAdd (data data) throws noaddexception{//1. Connect to the database. Try{//2, add data. //an exception occurred while adding data. throw new SQLException (); The program jumps, the disconnection is not performed. //The disconnection must be performed because the connection resource is wasted because it is not executed. //regardless of whether a problem occurs, you need to perform a disconnected action to free up resources. }Catch(SQLException e) {//solve the database problem. //also tell the caller about the problem. Throw Newnoaddexception ();}finally{//3. Disconnect the connection. }}

Summary: When is finally used ?
As long as the program uses the specific resources (database connection, IO resource, network connection socket, etc.)
Need to be released, all must be defined in finally. You are defining the program, as long as the problem occurs or not,
Specifies that the program needs to be executed when it is defined in the finally.

The combination of two anomalies


Try Catch finally combination mode:

1. Try catch : Makes exception detection of code and passes the detected exception to catch processing.

Try Catch : Abnormal detection of code and passing the detected exception to catch processing. Exception capture processing.  void Show ()//{try{throwNew Exception ();} Catch (Exception e) {}}

2. Try finally : Exception detection of the code, after an exception is detected because there is no catch, so the default JVM will be thrown.
The exception is not captured for processing. But the function's open resource needs to be closed, all finally.
Only to close the resource.

void Show ()//{try{thrownew  Exception ();} finally {}}

3,
Try Catch finally
Detects exceptions, passes them to catch processing, and defines resource deallocation.

4,try catch1 catch2 Catch3 ...

class ExceptionDemo10 {publicstaticvoid  main (string[] args) {System. out. println ("Hello world! " );}}

Three exceptions in the details of inheritance or implementation

details of the use of exceptions in inheritance or implementation: ★★★★★
1, when a subclass overrides a parent class method, if a method of the parent class declares an exception, the subclass can only declare the parent class exception or subclass of the exception, or not declare.
2, when a parent class method declares multiple exceptions, the subclass overrides only a subset of multiple exceptions.
3, when the overridden method has no exception declaration, the subclass overrides cannot declare the exception.
Example: This is the case with the parent class, and this is the case with the interface.
Problem: An exception was not declared in the interface, and an exception occurred while implementing the subclass override method.
 cannot make throws declaration, only catch capture. What if the problem can't be dealt with? The catch continues to throw, but the exception can only be converted to the RuntimeException subclass thrown .

1, when a subclass overrides a parent class method, if a method of the parent class declares an exception, the subclass can only declare the parent class exception or subclass of the exception, or not declare.

Cause: A type conversion exception occurs if this is not the case. See the code below for more detail

The following Red section produces a type conversion exception //aexception ex = new Bexception ();

PS: There is a problem with the parent class, the subclass cannot have more problems when overwriting

 Exception |--aexception |--aaexception |--bexception classaexception extends exception{}classbexception extends exception{}classaaexception extends aexception{}classfu{voidShow () {}}classtool{voidMethod (Fu F)//Fu f = new Zi ();    {        Try{f.show (); }        Catch(Aexception ex)//aexception ex = new Bexception ();{}}}tool t=NewTool ();//T.method (New Fu ());T.method (NewZi ());classZi extends fu{void Show () throws Bexception
{ } }

Four packs

Java-exception advanced-use of packages

Related Article

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.