Java Fundamentals (i) Exception handling keywords: Try Catch finally throw throws

Source: Internet
Author: User
Tags switch case throw exception try catch

Hi, everybody good evening, my blog first beginning, let's cheer together!

All say the Java language is very robust, such as: garbage collection mechanism, memory model, exception handling, strong type conversion, cross-platform, and so on, so that the Java language is favored. Let's talk about it today. Java exception handling mechanism try catch finally throw throws, usually we seem to underestimate the five keywords. Development of the application system, good exception handling of the system later development, maintenance, upgrade, user experience is particularly important.

Exceptions have a very important feature, from the location of the exception to the top of the main method if you have not catch it, eventually the JVM will help you throw exception information, the bad thing is that the thread is broken, the subsequent code is no longer executed, and silently disappeared in the JVM this vast ocean. Front of my company a project front-end Ajax request control to pay, because the control of the catch inside throws a null pointer, causing the front-end page stuck, the solution: because the control layer is generally the best catch to the top of any place where there may be anomalies, Other non-top layers can also continue to throw or throws upwards. There is also a better time-out for each AJAX setting.

Let's briefly introduce the following throw and throws:

Throw: Throw an exception in the method body, the actual existence of the exception object, the general use is the exception instance of its own extension, throws is placed after the method name, indicating that the method if there is an exception, I do not want to process or handle, to the caller processing, you can thow throw a runtime exception ( Unchecked) such as classnotfoundexception,numberfromartexception, you can also throws or Throw+try or throw+throws to handle a checked exception such as: I Oexcepion,socketexception, inheriting exceptions such as the Exception class. The difference is that the checked exception must be handled (either try, or throws continue to throw up, otherwise the compilation is not possible), and the runtime exception can not be handled, and the consequence of the failure is that the JVM reports exception information after an exception and then the thread breaks off. Throw and throws keywords are generally not recommended for use in code, and all exceptions are recommended for in-place resolution. Know the use on the line, do not do too much explanation.

The combination rule for try: 1, Try{}catch () {} 2,try{}catch () {}finally{} 3,try{}finally{}, 1 and 2 way catch can have multiple

Friends, eat a few chestnuts:

1, no try Combination

public class Catchexecutejustone {
public void MethodOne () {
System.out.println ("Into MethodOne method");
int one=1/0;
System.out.println ("End MethodOne method"); does not output no try combination, the thread has been broken after an error
}
public static void Main (string[] args) {
Catchexecutejustones Cejo = new Catchexecutejustones ();
Cejo.methodone ();

SYSTEM.OUT.PRINTLN ("End Main Method"); does not output no try combination error thread has been broken
}
}

Output:

Into MethodOne method
Exception in thread "main" java.lang.ArithmeticException:/By zero
At Priv.lilei.exception.example_1.CatchExecuteJustOneS.methodOne (catchexecutejustones.java:6)
At Priv.lilei.exception.example_1.CatchExecuteJustOne.main (catchexecutejustone.java:19)

2.1, there is a try combination case 1

public class Catchexecutejustone {
public void MethodOne () {
System.out.println ("Into MethodOne method");
try{
int one=1/0;
}catch (Exception e) {
System.out.println ("MethodOne try to");
}
System.out.println ("End MethodOne method");
}

public static void Main (string[] args) {
Catchexecutejustone Cejo = new Catchexecutejustone ();
Cejo.methodone ();
SYSTEM.OUT.PRINTLN ("End Main Method");
}
}

Output:

Into MethodOne method
MethodOne try to
End MethodOne Method
End Main Method

2.2, there is a try combination case 2

public class Catchexecutejustone {
public void MethodOne () {
System.out.println ("Into MethodOne method");
int one=1/0;
System.out.println ("End MethodOne method"); Does not execute thread above the error is broken directly thrown out
}

public static void Main (string[] args) {
try{
Catchexecutejustone Cejo = new Catchexecutejustone ();
Cejo.methodone ();
}catch (Exception Exception) {
SYSTEM.OUT.PRINTLN ("Into Main method catch"); Performs a try-to-report exception to the above method
}
SYSTEM.OUT.PRINTLN ("End Main Method"); Performs a try-to-report exception to the above method
}
}

Output:

Into MethodOne method
Into Main method catch
End Main Method

2.3, there is a try case combination 3 exception that is only recently caught by its catch once. Like switch case with if () if Else () {} if () Else if{} syntax

public class Catchexecutejustone {
public void MethodOne () {
System.out.println ("Into MethodOne method");
try{
int one=1/0;
}catch (ArithmeticException e) {
System.out.println ("Catch 1");
}catch (Exception e) {
System.out.println ("Catch 2");//Does not execute the previous catch 1
}
}

public static void Main (string[] args) {
Catchexecutejustone Cejo = new Catchexecutejustone ();

try {
Cejo.methodone ();
} catch (Exception e) {
System.out.println ("Man Catch");//Do not execute catch 1
}

SYSTEM.OUT.PRINTLN ("End Main Method");
}
}

Output:

Into MethodOne method
Catch 1
End Main Method

2.4 There is a try combination case 4, try{}finally{} combination, finally no return is worth the time the thread will break, but in the finally there is a return value when the thread will not be broken just follow-up code will not be executed, this combination is less recommended.

No return value

public class Catchexecutejustone {
public void MethodOne () {//No return value
System.out.println ("Into MethodOne method");
try{
int one=1/0;
}finally{
System.out.println ("into MethodOne finally");
}
System.out.println ("End MethodOne method"); Does not execute thread above the error is broken directly thrown out
}

public static void Main (string[] args) {
Catchexecutejustone Cejo = new Catchexecutejustone ();
Cejo.methodone ();
SYSTEM.OUT.PRINTLN ("End Main Method");//does not execute thread above the error is broken directly thrown out
}
}

Output with no return value:

Into MethodOne method
Exception in thread "main" into MethodOne finally
Java.lang.ArithmeticException:/By zero
At Priv.lilei.exception.example_1.CatchExecuteJustOne.methodOne (catchexecutejustone.java:14)
At Priv.lilei.exception.example_1.CatchExecuteJustOne.main (catchexecutejustone.java:23)

Has a return value:

public class Catchexecutejustone {
Public String MethodOne () {
System.out.println ("Into MethodOne method");
try{
System.out.println ("1");
int one=1/0;
System.out.println ("2");//will not execute thread above the error is broken directly thrown out
}finally{
System.out.println ("into MethodOne finally");//Outputs
return "1";
}
}

public static void Main (string[] args) {
Catchexecutejustone Cejo = new Catchexecutejustone ();
Cejo.methodone ();
SYSTEM.OUT.PRINTLN ("End Main Method");//will execute because there is a try to and the method has a return value
}
}

Output with return value:

Into MethodOne method
1
into MethodOne finally
End Main Method

2.5, the combination of finally and finally is always executed, and there is a return worth the situation to perform before returning unless there is a particularly violent act such as system.exit (0); Or it's broken, or the memory overflows, and so on error.

return combination

2.5.1 The following two cases in the case of no exception and exception, there is a difference in the assignment of variables to the variable in catch and finally. No exception was assigned again and the exception was successfully assigned again.

1 No abnormal conditions

public class Catchexecutejustone {
Public String MethodOne () {
String a= "";
System.out.println ("Into MethodOne method");
try{
A= "a";
return A;
}catch (ArithmeticException e) {
System.out.println ("Catch 1");
}finally {
SYSTEM.OUT.PRINTLN (1);
A= "A2"; No error will be assigned to A;
SYSTEM.OUT.PRINTLN (2);
}
SYSTEM.OUT.PRINTLN (3); Does not execute the above return a method has returned
return A;
}

public static void Main (string[] args) {
Catchexecutejustone Cejo = new Catchexecutejustone ();
System.out.println (Cejo.methodone ());
}
}

The output of the try in return without exception:

Into MethodOne method
1
2
A

2 with unusual conditions

public class Catchexecutejustone {
Public String MethodOne () {
String a= "";
System.out.println ("Into MethodOne method");
try{
A= "a";
int i=1/0;
return A;
}catch (ArithmeticException e) {
System.out.println ("Catch 1");
}finally {
SYSTEM.OUT.PRINTLN (1);
A= "A2"; An exception will be re-assigned to the A variable
SYSTEM.OUT.PRINTLN (2);
}
SYSTEM.OUT.PRINTLN (3); The output snaps to an exception not returned from the first return a above, but from the following return
return A;
}

public static void Main (string[] args) {
Catchexecutejustone Cejo = new Catchexecutejustone ();
System.out.println (Cejo.methodone ());
}
}

The return in try has an abnormal condition output:

Into MethodOne method
Catch 1
1
2
3
A2

Java Fundamentals (i) Exception handling keywords: Try Catch finally throw 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.