Home >Java >javaTutorial >What does try mean in java
try is a keyword in Java, mainly used for exception handling mechanism, so what does it do?
try – used for listening. Place the code to be monitored (code that may throw exceptions) within the try statement block. When an exception occurs within the try statement block, the exception is thrown.
It is generally used in combination with the catch..finally block to guide the first part of the keyword and is used to declare the exception that needs to catch the specified statement block.
The complete usage method is:
try { 语句块1 } catch (<? extends Throwable> e) { 语句块2.1 } catch (<? extends Throwable> e) { 语句块2.2 } catch (<? extends Throwable> e) { 语句块2.3 ... } finally { 语句块3 }
catch can appear 0, 1 or more times, finally can appear 0 or 1 times, but catch and finally They cannot both appear at the same time.
Example:
public class Test { public static void main(String[] args) { int i = 10; try { System.out.println(i / 0);//会出现异常 } catch(ArithmeticException ame) { ame.printStackTrace(); } finally { System.out.println("byebye"); } } }
Related learning recommendations: java basic tutorial
The above is the detailed content of What does try mean in java. For more information, please follow other related articles on the PHP Chinese website!