Java The return values in exception handling are different in different situations , We need to pay more attention here

There are two scenarios :

  • 1 finally Statement block does not have return sentence , That is, when the code is executed to try perhaps catch In statement block return When the sentence is , It is equivalent to saving a local copy of the value of the variable that returns the value at this time , And then according to try catch finally To execute finally Sentence block , Then go back and return the copy data you just saved to

The first example is in try There is no exception in , Go straight to try In statement block return scene

public class Test01 {
public static void main(String[] args) {
int results=new Test01().test(10,2);
System.out.println("func results:"+results);
} public int test(int a,int b)
{
int results=0;
try
{
System.out.println("in try.....");
results+=1;
System.out.println("results = "+results);
return results;
}
catch (Exception e)
{
System.out.println("in catch......");
results+=1;
System.out.println("results = "+results);
return results;
}
finally {
System.out.println("in finally");
results+=1;
System.out.println("results = "+results);
}
}
}

The results are as follows , The last value returned by the function is 1, instead of 2

in try.....
results = 1
in finally
results = 2
func results:1

The second example is try There's an exception in , But in catch I'm here in the middle return sentence

public class Test01 {
public static void main(String[] args) {
int results=new Test01().test(10,2);
System.out.println("func results:"+results);
} public int test(int a,int b)
{
int results=0;
try
{
System.out.println("in try.....");
results+=1;
System.out.println("results = "+results);
double d=1/0;
return results;
}
catch (Exception e)
{
System.out.println("in catch......");
results+=1;
System.out.println("results = "+results);
return results;
}
finally {
System.out.println("in finally");
results+=1;
System.out.println("results = "+results);
}
}
}

The return value is as follows , That is to say, the return value is 2 instead of 3

in try.....
results = 1
in catch......
results = 2
in finally
results = 3
func results:2
  • 2 finally The statement block has return sentence , At this point, I finally return to finally The value in the statement block , and try perhaps catch There is... In the statement block return sentence , And it won't work

The following example is try I can walk to return sentence , Again finally There are also return At the time of the statement , Return at this time finally In statement block return Value

public class Test01 {
public static void main(String[] args) {
int results=new Test01().test(10,2);
System.out.println("func results:"+results);
} public int test(int a,int b)
{
int results=0;
try
{
System.out.println("in try.....");
results+=1;
System.out.println("results = "+results);
return results;
}
catch (Exception e)
{
System.out.println("in catch......");
results+=1;
System.out.println("results = "+results);
return results;
}
finally {
System.out.println("in finally");
results+=1;
System.out.println("results = "+results);
return results;
}
}
}

The results are as follows , return 2, This is not a return 1 了

in try.....
results = 1
in finally
results = 2
func results:2

The following example is in catch Go to the return sentence , Also in finally In the statement block, we also come to return sentence , This is also a return finally The value in the statement block ,catch Invalid return value in statement block

public class Test01 {
public static void main(String[] args) {
int results=new Test01().test(10,2);
System.out.println("func results:"+results);
} public int test(int a,int b)
{
int results=0;
try
{
System.out.println("in try.....");
results+=1;
System.out.println("results = "+results);
double d=1/0;
return results;
}
catch (Exception e)
{
System.out.println("in catch......");
results+=1;
System.out.println("results = "+results);
return results;
}
finally {
System.out.println("in finally");
results+=1;
System.out.println("results = "+results);
return results;
}
}
}

The results are as follows : return 3 instead of 2 了

in try.....
results = 1
in catch......
results = 2
in finally
results = 3
func results:3

Java Exception handling scenarios in different locations of the return value detailed analysis of more related articles

  1. Why Alibaba Java It is mandatory in the development manual that the return value of the interface is not allowed to use enumeration ?

    In the reading < Alibaba Java Development Manual > when , It is found that there is a rule that enumeration type is not allowed for interface return value in two party library dependency , The details are as follows : Before we talk about why, let's talk about what is a two-way library , A two-way library is also called a two-way package , In general, it refers to ...

  2. JAVA finally Analysis of abnormal loss and return value coverage of sentences

    Reprint :http://blog.csdn.net/sureyonder/article/details/5560538 Java Virtual machines are in every try Statement block and its related catch The end of a clause   Every city “ call ”f ...

  3. For class network -Java Season one -7-3 Java The use of the return value method without parameters in

    source :http://www.imooc.com/code/1579 If the method does not contain parameters , But there is a return value , We call it the parameterless method with a return value . for example : The following code , Defines a method called calSum , No parameter , But the return value is ...

  4. Java The use of the return value method without parameters in

    If the method does not contain parameters , But there is a return value , We call it the parameterless method with a return value . for example : The following code , Defines a method called calSum , No parameter , But the return value is int Method of type , The operation performed is to calculate the sum of the two numbers , And return the result stay c ...

  5. Java and Ibatis Call the stored procedure and get the return value

    Java and Ibatis Call the stored procedure and get the return value 2011-07-19 17:33 jiandanfeng2 CSDN Blog   Font size :T | T This paper mainly introduces Java and Ibatis Method to call stored procedure , ...

  6. python in os.system() The return value of

    [python in os.system() The return value of ] If the third party program returns a Boolean return value ,os.system Will true To 1,false To 0 Go back . problem : /bin/xxx.py Is a return code for 1 Of ...

  7. PHP Use in CURL And php curl Detailed analysis and common pit

    This article mainly introduces PHP Use in CURL And php curl Detailed analysis and common pit , Now share with you , Also give you a reference . Let's follow Xiaobian to have a look Tanabata , As a development , If you don't have to tease me “ Flirt ” Get off the server , My sister has wonderful classmates ...

  8. Java The use of the return value method with parameters in

    If the method contains both parameters , With return value , We call it a method with parameters and return values . for example : The following code , Defined a show Method , With a parameter name , Method returns a String Result of type Call... With parameters and return values ...

  9. java exception handling -finally Use in return and throw sentence

    java In the exception statement finally Blocks are often used for resource release operations , Such as closing files . Turn off network connection . Close database connection, etc . Under normal circumstances finally Statement should not use return Statements should not throw exceptions , The following discussion is limited to java ...

  10. JAVA Exception handling analysis ( in )

    In the use of java In exception handling mechanism, we will find that some exceptions can be thrown without grabbing , And some exceptions must be crawled , What is the situation ? Design concept guess :       There are some anomalies in the scene , It doesn't need to be dealt with or often doesn't ...

Random recommendation

  1. php Design patterns -- The singleton pattern

    The singleton pattern (Singleton Pattern  Singleton mode or singleton mode ) The singleton pattern ensures that a class has only one instance , And instantiate it yourself and provide it to the entire system . Singleton pattern is a common design pattern , In computer system , Thread pool . slow ...

  2. IIS: Log code analysis

    How to see IIS Log code , open IIS After the log , You'll see a lot of interviews in it .baiduspider,Googlebot Wait for the spider . Spiders leave records after they climb , The status code is listed below : 100 - A message indicating that a request has been received ...

  3. MTOM And in WCF Application in

    About MTOM Basic concepts of mention MTOM Message optimization transmission mechanism , The usual experimental result is to use MTOM Transmitting data will increase by about 33% Performance of . Message transmission optimization mechanism (MTOM) The standard allows externalization of large data elements contained in messages , And use it as ...

  4. TestPointer

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  5. FPGA Applications in other fields ( Four )

    Industry : From factory and process automation to energy infrastructure and machine vision systems , Industrial products help to improve our world . The product must be safe . reliable . Strong adaptability , And durable . meanwhile , Business success requires you to act quickly in a highly competitive market , At the same time, reduce the total cost . Internet ...

  6. use Python Grab the page and parse

    Software version python:2.7.12 Web crawling Library Web crawler library is requests, github The address is :https://github.com/requests/requests, The document address is :http: ...

  7. vue Components

    Components have local components and global components , Global components , Other elements can call . Prop The parent component and the child component don't understand .

  8. es6 Template string

    Template string Provides syntax for constructing strings , stay Prel/python And so on . 1. Back quote template , You can change lines 2. Back quote template , Can be nested use +`` To nest benefits : The grammar is more concise var name=&q ...

  9. Lua Common encapsulation methods

    Lua Get random values -- Get random values , Specify upper and lower limits function getRandom(min,max) -- Receive an integer n As seeds of random sequences math.randomseed(os.time()) ...

  10. The function uses twelve :BAPI_MATERIAL_BOM_GROUP_CREATE(CS61)

    REPORT ZSM_CREATE_SIMPLEBOM.* This code will create a material BoM for the material* MAINMATERIAL wi ...