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);
}
}