How Java solves exceptions that should be thrown or should be handled

Source: Internet
Author: User

The case is, Q g () should throw an exception of f () or the exception of F ()?

If H () does not need to know if G () is successful or if there is an error, or if H () fails to control or solve the error of G (), then G () does not need to report an exception to H ()

Second, if H () needs to know if G () is successful in operation and can handle the error of G (), then g () cannot handle the exception itself and should be thrown upwards.

Three, G () can treat the exception of F () as follows:

Private void g () {

Try {

f ();

} catch (Exception e) {

Throw New RuntimeException ("RuntimeException in G ()!", e);

}

}

In this way, H () can choose to handle exceptions thrown in g (), such as:

Private void h () {

Try {

g ();

} catch(RuntimeException re) {

Try {

Throw (Exception) Re.getcause ();//Find out the original exception

} catch(Exception e) {

System. out. println ("G () Exception in H () have been dealt");//handling the original exception

}

}

}

You can also discard handling of the exception directly, at which point the Java compiler does not force the request to handle the exception in G ()!

In summary, whether G () continues to throw an exception, which in itself does not depend on itself, but depends on the upper calling function

Package inputfile;

import Java.io.BufferedReader;

import java.io.FileNotFoundException;

import Java.io.FileReader;

import java.io.IOException;

Public class Inputfile {

Private BufferedReader in;//Represents resources

constructor, you should be responsible for the resource release problem when resource open fails, it should only be represented to the upper layer

If the resource is open successfully, other issues of success or not should not be addressed by the senior

Public Inputfile (String fname) throws Exception {

Try {

in= New BufferedReader (new FileReader (fname));//Open Resource

} catch (FileNotFoundException e) {

System. out. println ("Could not Open" +fname);

throw E;

} catch(Exception e) {

Try {

In.close ();

} Catch (IOException E1) {

System. out. println ("In.close () unsuccessful");

}

throw E;

}

}

The resource handler function, if processing an error or if a special case occurs, it is reasonable to let the upper level know the process

What has happened so that the upper class can take further steps

Public String GetLine () throws ioexception{

String s;

S= In.readline ();

return s;

}

The resource deallocation function should not throw an exception, because this class is already the underlying class for processing resources, if

Even if the class does not know how to release the resources, even if the top throws an exception, the upper layer does not know how to handle

Public void Dispose () {

Try {

In.close ();

} catch (IOException e) {

Throw New RuntimeException ("In.close () failed!");

}

}

}

Package inputfile;

Public class Test {

Public Static void Main (string[] args) {

Try {

Inputfile input=New inputfile ("DFJKD");

Try {

String s=input. GetLine ();

} catch(Exception e) {

E.printstacktrace ();

} finally{

Input.dispose ();

}

} catch (Exception e) {

E.printstacktrace ();

}

}

}

How Java solves exceptions that should be thrown or should be handled

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.