The use rules of throws and throws that throw exceptions in JAVA and their differences

Source: Internet
Author: User
Tags exception handling getmessage

Throw: (object-oriented approach)


Throw an exception, either defined by the system or by yourself. The following are two examples:
   
Throw a system exception in Java:


Public class One {
Public void yichang (){
NumberFormatException e = new NumberFormatException ();
Throw e;
}
Public static void main (String [] args ){
One test = new One ();
Try {
Test. yichang ();
} Catch (NumberFormatException e ){
System. out. println (e. getMessage ());
}
}
}
 


 
Throw a custom exception:
 

Public class People {
Public static int check (String strage) throws MyException {
Int age = Integer. parseInt (strage );
If (age <0 ){
Throw new MyException ("age cannot be negative! ");
 }
Return age;
 }
Public static void main (String [] args ){
Try {
Int myage = check ("-101 ");
System. out. println (myage );
} Catch (NumberFormatException e ){
System. out. println ("Data Format Error ");
System. out. println ("reason:" + e. getMessage ());
} Catch (MyException e ){
System. out. println ("data logic error ");
System. out. println ("reason:" + e. getMessage ());
 }
 }
}
Public class MyException extends Exception {
Private static final long serialVersionUID = 1L;
Private String name;
Public MyException (String name ){
This. name = name;
 }
Public String getMessage (){
Return this. name;
 }
}

 
 
Throws: (an exception thrown for a method)


Throw an exception, either defined by the system or by yourself.
 
Throw a system exception in java:

Public class One {
Public void yichang () throws NumberFormatException {
Int a = Integer. parseInt ("10L ");
}
Public static void main (String [] args ){
One test = new One ();
Try {
Test. yichang ();
} Catch (NumberFormatException e ){
System. out. println (e. getMessage ());
}
}
}
 


 
Throw a custom exception:
 

Public class People {
Public static int check (String strage) throws MyException {
Int age = Integer. parseInt (strage );
If (age <0 ){
Throw new MyException ("age cannot be negative! ");
}
Return age;
}
Public static void main (String [] args ){
Try {
Int myage = check ("-101 ");
System. out. println (myage );
} Catch (NumberFormatException e ){
System. out. println ("Data Format Error ");
System. out. println ("reason:" + e. getMessage ());
} Catch (MyException e ){
System. out. println ("data logic error ");
System. out. println ("reason:" + e. getMessage ());
}
}
}
 
Public class MyException extends Exception {
Private static final long serialVersionUID = 1L;
Private String name;
Public MyException (String name ){
This. name = name;
}
Public String getMessage (){
Return this. name;
}
}
 


 
Then I will explain when to use it:
 
If a system exception occurs, you do not need to do anything. You can also throw an exception to the method because the system exception can be automatically captured by the system, therefore, whether this exception should be solved within the method or by the upper-layer function is actually the same. However, I checked a lot of information. Even if an exception is thrown and can be captured by the system, it is recommended to write a throws for the method, this allows other programmers to know what exceptions will occur when a large task is completed.
 
If the exception is defined by yourself, throws must be used to throw the exception that may be thrown by this method; otherwise, an error will be reported during compilation.



What is the difference between throws and throw exceptions?

There are obvious differences. First, it is the position used. throws can only follow the method name and brackets, while throw can only appear in the method body. Second, throws is a declaration (it declares that exceptions may occur, but may not always occur), and throw is an action (it throws and can be said that it generates an exception, if this keyword is executed, an exception will occur ). You may be familiar with throws, and throws do not know how to use them. Let me talk about it more specifically. When throw is used to throw (generate) an exception, if throws is used to discard the exception, the exception will be thrown to the place where the method is called. If this method does not discard the exception with throws, you need to use try... catch to catch the exception. I believe you can understand the difference between "generate" and "abandon. Is where an exception occurs. Abandon is to let the exception go to another place, where it passes. If you still don't understand the specific use of throw, let me give you another simple example. For example, if you want to create a program and enter the month, if you enter 13, it is invalid data. At this time, you have two processing methods. The first one is to use the judgment statement to judge, then the prompt is displayed. The second one is to throw an exception and let the upper-layer call methods handle this input error. If you do not understand the advantages of this practice, it doesn't matter. You will find it when you do more programs. Now you know that it has such usage.


1. throws is used to throw an exception at the method level and handle the exception directly by calling some methods. Therefore, throws are often used after the method. For example
Public static void main (String [] args) throws SQLException

2. throw is used for the code in the method block, which is lower than the throws level, such as try... catch .... statement block, indicating that it throws an Exception, but it does not process it. Instead, it calls the Exception handling class to handle the throws Exception of the method block.

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.