JAVA exception throw and throws

Source: Internet
Author: User
Tags finally block try catch

Recently has been throw and throw new ... strike, to be thoroughly angry clear this matter, he to this thought essence collects a lot of netizens. Excerpt here.

Throws all exception information
A throw is a detailed exception type thrown.
The exception information that can be thrown by a method (class) is typically declared at the declaration of a Method (Class) by throws. In the method (class), a throw is used to declare a detailed exception information.
Throws usually does not show the catch exception, the system can voluntarily throw all the caught exception information to the superior method;
Throw will require the user to catch the relevant exception, and then in the relevant packaging, and finally the package after the exception information thrown.
the exception is handled differently . Throws the exception does not handle, who calls who deal with, throws the value range of exception is greater than the maximum range of the inner exception of the method, and the Cathch range is greater than throws exception range; The throw actively throws itself to define the exception class object. The throws throws the class, and the throw throws the object.

In the method definition is the statement tone, the third person singular, throw obviously to add s.

(throws generally used as a clause for method definitions)
In the function body to use throw, is actually imperative + emphasis, equivalent to do throw ..., do + verb prototype

Throw is used to throw an exception. You can throw a pre-defined exception and your own definition exception.

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

A throw is a "verb". Immediately after the try statement block.


and throws is a "noun", used after the function method name functions A () throws Exception e {}
Throw is used in the program to clearly indicate that an exception is thrown here. Throws used in the place of the method declaration. Indicates that this method may throw an exception.


Throw is used to throw the actual exception, followed by an exception object (instance), is an actual statement
Throws is used to declare, after a method declaration, followed by the name of an exception class, representing a general action rather than a particular action.

Using throws is to illustrate that the current function throws an exception.


In general cases, some of the functions you call throw some exceptions. But you do not want to handle it in the current context, and you can declare that the function throws the exception. So you don't have to try-catch it. When the exception occurs, the function throws this exception, allowing the previous layer of the function to be processed. Throws also known as Exception Specification
public static H () throws
{
     try
     {
         A.G ();
    }
     catch (Exception e)
     {
          throw E;
    }
}
Because you did not handle this exception. But simply thrown out.
and simple declaration
public Static H () throws
{
     a.g ();
}
will also throw this exception
II) try Catch/throws/throw

Throws assumes that a corresponding error has occurred. The bottom is indeed not to be run;
The understanding of Try catch should be dialectical: assuming that the catch does not throw out again, it will continue to run. To not run must throw processing
Throws throws an exception. Can not solve the upward, straight touch to solve the exception of the processing program, as if your boss let you run a task, halfway through the problem you do not know how to solve. You return the question to your boss. Think that since it is the task assigned by T should know how to solve the problem, your boss can not solve the same to the manager to solve. Up in turn. Until someone can solve the problem (do not want to write another code to handle the exception is very useful)
Try catch is to consider the possibility that the try includes this code may encounter such an exception. Capture processing directly with catch. Catch includes code for handling code

Throws just throws an exception, assuming that your upper-level code has a way of handling it, and that the exception is handled by the upper-level code.
Try/catch is clearly aware of what might happen to the operation, and there should be a way to handle it in the catch block at the same time.
And the other way is try/catch/finaly.

Throws is to return the exception to the caller, which is handled by the caller, or the caller is going to be try/catch, not run away.
The catch is a simple SYSTEM.OUT.PRINTLN (...) ..... ); and. Connection to the database will not be connected, you do not know whether it is a drive problem, computer problems or network problems, sent to the user. The user also can not understand, so all throws to catch, prompt please contact the administrator.

。。

。。

。 That's a lot easier.

Throws is written after the method signature.
Throw is written in the method body and can be written in the IF () ....
Could catch a exception and throw him out immediately. What do not do, also can catch live after throw new A your custom exception ....

Throws is to throw the exception, but in the future there will be a catch to accept, or throw it to the main function. It's going to go up one level at a time. Until there was a acceptance of his

Throws throws an exception to the method handling that calls the method, i.e.:
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!");
}
}

It's obvious that the answer is:
Invoke the method begin!
Method begin!
Catch exception!

The finally statement is optional. A try statement must have at least one catch or one finally,finally statement to provide a uniform exit for exception handling, and the statements in the finally block will be run regardless of whether the try code block has an exception event
Declaring an exception in an overridden method
In the subclass. If you want to overwrite a method of the parent class, or a method in the parent class declares a throws exception, the method of the subclass can also throw an exception. But remember that the exception thrown by the subclass method can be just the same or subclass of the exception that the parent method throws.
Such as:
Import java.io.*;
Class A {
public void MethodA () throws ioexception{
.....
}
}
Class B1 extends A {
public void MethodA () throws filenotfoundexception{
....}
}
Class B2 extends A {
public void MethodA () throws Exception{//error
....}
}


public void Method () throws Exception {
try {
Detailed procedures
} catch (Exception ex) {

}
}
Let's say the detailed procedure goes wrong. It will deal with the catch in the following program body, this time throws Exception is actually meaningless.
public void Method () throws Exception {
try {
Detailed procedures
} catch (FileNotFoundException ex) {

}
}

Assuming that the detailed program is faulted, and is filenotfoundexception, it will handle the catch in the following program body.
At this time the Exception beyond FileNotFoundException will be throws Exception and throw to the upper layer.

Throw is written in the method body, and throws is written at the back of the method name
Throwkeyword format: throw new ArithmeticException (); Throws an exception. These anomalies can make unchecked exception (also known as runtimeexception), and can be checked execption. The throw must have a try/catch statement that catches the exception
Format of the Throwskeyword
private void Arraymethod (int[] arr)
Throws ArrayIndexOutOfBoundsException,
ArithmeticException {
Body
}
The throws clause lists the types of exceptions that a method might throw. In addition to the error and RuntimeException exceptions, the exceptions that may be thrown in the method must be declared in the throws list, or there will be a compilation error.


For example: If the method might throw illegalaccessexception (belonging to checked execption) You must throws the statement list.

System exception is thrown by default, to display a custom exception thrown

The others are just a library method of throw without treatment, so that on the surface you don't see throw and catch exception

JAVA exception throw and throws

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.