zoukankan      html  css  js  c++  java
  • 线程异常测试

    import java.lang.Thread.UncaughtExceptionHandler;
    
    public class ThreadException
    {
    	public static void main(String[] args)
    	{
    		try
    		{
    			MyThread t1 = new MyThread(0);
    			t1.start();
    			MyThread t2 = new MyThread(1);
    			t2.start();
    		}
    		catch (Exception e)
    		{
    			System.out.println(e.getMessage());
    		}
    		while(true);
    
    	}
    }
    
    class MyThread extends Thread
    {
    	int i;
    	public MyThread(int i)
    	{
    		this.i = i;
    	}
    
    	public void run()
    	{
    		if (i == 0) throw new RuntimeException(i + "");
    		else
    			System.out.println("thead end");
    	}
    }
    

      结果

    Exception in thread "Thread-0" java.lang.RuntimeException: 0
    	at MyThread.run(ThreadException.java:33)
    thead end
    

      异常被封闭在当前线程,不会抛到开启线程的方法中来

  • 相关阅读:
    Go语言基础--1.1 变量的声明
    基本语法
    弹性盒子修改
    弹性盒子内容
    弹性盒子
    响应式列重置
    栅格系统
    布局容器
    额外按钮
    可消失的弹出框
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6852987.html
Copyright © 2011-2022 走看看