异常:

1、异常类型

package com.tzxy;

public class Error {
	//异常处理
	public static void main(String[] args) {
	//	System.out.println("梦想照亮现实") 报错信息:
		/*Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Syntax error, insert ";" to complete BlockStatements

	at com.tzxy.Error.main(Error.java:6)
		 * 
		 */
	String str = "lili";
	System.out.println(str+"年龄是:");
	int age = Integer.parseInt("20L");
	System.out.println(age);
	}
	/*
	 * Exception in thread "main" java.lang.NumberFormatException: For input string: "20L"
	at java.lang.NumberFormatException.forInputString(Unknown Source):
	NumberFormatException:当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常		
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at com.tzxy.Error.main(Error.java:15)
	 */
}

异常的父类都是Exception和Error。
可以通过JDKAPI查找
2、tyr…catch

package com.tzxy;

public class FootballTeam {
	private int playerNum;
	private String teamName;
	public FootballTeam(){
		try {
			Class.forName("com.tzxy.Footballcsaw");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();//获取异常事件发生时执行堆栈的内容
			e.toString();//获取异常的类型和性质
			e.getMessage();//获取有关异常事件的信息
		}
		/*
		 * 返回与带有给定字符串名的类或接口相关联的 Class 对象。调用此方法等效于: 
  Class.forName(className, true, currentLoader)
 其中 currentLoader 表示当前类的定义类加载器。 
例如,以下代码片段返回命名为 java.lang.Thread 的类的运行时 Class 描述符。 

   Class t = Class.forName("java.lang.Thread")

		 */
	}
	public static void main(String[] args) {
		FootballTeam team = new FootballTeam();
		team.teamName = "com.tzxy";
		team.playerNum = 19;
		System.out.println("\n"+"球队名称:"+team.teamName+"\n"+"球员数量:"+team.playerNum);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值