try catch是干嘛的 在什么情况下使用 catch是否有多种写法

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

try catch是干嘛的 在什么情况下使用 catch是否有多种写法

用来捕获异常,给你个例子

import java.io.*;
public class ExcepTest{
 
   public static void main(String args[]){
      try{
         int a[] = new int[2];
         System.out.println("Access element three :" + a[3]);
      }catch(ArrayIndexOutOfBoundsException e){
         System.out.println("Exception thrown  :" + e);
      }
      System.out.println("Out of the block");
   }
}


try{
  //代码块
}catch(){
  //处理代码块的异常
}finally{
  //无论怎么样都执行,除非程序结束了
}

捕获异常
https://blog.csdn.net/m0_64491107/article/details/122444009