If an error occur, use try...catch to catch the error and execute some code to handle it:
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
The catch
keyword catches exceptions generated by try
statements.
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
Read more about exceptions in our Java Try..Catch Tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!