java异常处理

来源:互联网 发布:vscode怎么编译运行 编辑:程序博客网 时间:2024/06/05 19:38
import java.util.*;
import java.math.*;
  public class Main {
   
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    Main m1 = new Main();
    try
    {
    m1.test2();
    }catch(Exception e)
    {
    e.printStackTrace();
    }
   
    }



public void test1() throws Exception
{
throw new Exception("喝酒就开车!!");
}
public void test2()
{
try {
test1();//抛出异常
} catch (Exception e) {

e.printStackTrace();
RuntimeException newEXC = new RuntimeException("sisisisisi");定义一运行时异常或者直接是new RuntimeException(e);删掉下一句
newEXC.initCause(e);//把test1抛出的变为运行时异常
throw newEXC;//抛出异常
}
}
   
}
0 0
原创粉丝点击