1.import

package com.yfs.javase;

import java.util.Scanner;

//import java.lang.String;//默认导入
public class Demo1 { public static void main(String[] args) {
String s = new String("abc");//java.lang.String
String s1 = "abc"; System.out.println("影分身");//输出
Scanner scan = new Scanner(System.in);//输入
System.err.println("我没错...");//错误流 long now = System.currentTimeMillis();
System.out.println("系统时间 " + String.format("%tF %<tT", now));
//System.exit(0); System.out.println (System.getenv("java_home")); System.out.println("程序执行到此..."); } }

2.异常

package com.yfs.javase;
//异常
//执行出现异常 jvm 终止代码执行
//报告错误原因
//机制
public class ExpDemo1 { public static void main(String[] args) {
String s = null;//没有对象
//s.toString();//NullpointException
int[] a = new int[5];
//a[5] = 20; //ArrayIndexOutOfBoundsException int b = 3;
int c = 0;
int d = b / c;//ArithmeticException System.out.println("程序执行结束...");
} }

3.运行时异常

Person 类

package com.yfs.javase;

public class Person {

	private String name;
private int age;
private char sex; public Person() { } public Person(String name) {
this.name = name;
} public Person(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
} public void introduce() {
System.out.println("I am Person....");
} public String toString() {
return "姓名:" + name + " 年龄 :" + age + " 性别:" + sex;
} public void speak() {
System.out.println(name + " 工件了吗?");
} public void sleep() {
System.out.println(name + " 睡觉了吗?");
} public void eat() {
System.out.println(name + " 吃了吗?");
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public char getSex() {
return sex;
} public void setSex(char sex) {
this.sex = sex;
} }

类转换异常

package com.yfs.javase;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream; //异常分类 public class ExpDemo2 { public static void main(String[] args) {
// 运行时异常
Object obj = new Person();
// String s = (String)obj;//ClassCastException // check异常 编译时强制处理
try {
InputStream in = new FileInputStream("123.txt");
} catch (FileNotFoundException e) {
System.out.println("异常处理完成...");
e.printStackTrace();
} System.out.println("程序执行结束...");
} }

4.抛异常

package com.yfs.javase;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException; //抛异常 public class ExpDemo3 { public static void main(String[] args) throws FileNotFoundException {
// 谁调用谁处理 没有对象处理 jvm处理 终止程序
// try {
try {
test();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
} // 在方法抛出异常 throws 可以抛多个异常
public static void test() throws FileNotFoundException, SQLException {
int[] a = new int[3];
a[3] = 15;// 系统检查抛出异常 // 自己抛 throw
if (1 == 1) {
throw new ArithmeticException("故意抛异常");
}
// check 异常
InputStream in = new FileInputStream("123.txt");
}
}

5.异常处理

package com.yfs.javase;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException; //异常处理 catch 捕获异常对象 处理异常 public class ExpDemo4 { public static void main(String[] args) {
String s = "abc";
int[] a = new int[3];
//try...catch...finally
try{
a[3] = 15;//jvm创建异常对象 抛出异常 处理第一个异常
s.toString();
//可以有多个catch语句块
}
// catch (ArrayIndexOutOfBoundsException e) {//捕获异常
// //e.printStackTrace();
// System.out.println("我知道了..我是故意的");
// } catch (NullPointerException e) {
// System.out.println("字符串异常处理完成...");
// } catch (Exception e) {
// e.printStackTrace();
// // finally 总会执行
// }
finally {
System.out.println("总是执行....");
} System.out.println("程序执行结束...");
} }

6.自定义异常

package com.yfs.javase;
//自定义异常
public class ExpSelf extends Exception { public ExpSelf() {
super();
// TODO Auto-generated constructor stub
} public ExpSelf(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
} public ExpSelf(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} public ExpSelf(String message) {
super(message);
// TODO Auto-generated constructor stub
} public ExpSelf(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
//String参数的构造方法
// public ExpSelf(String msg) {
// super(msg);//调用父类构造方法
// } }

7.自定义异常 测试

package com.yfs.javase;

import java.io.IOException;

public class Test {

	public static void main(String[] args) {
try {
if( 1 == 1) {
throw new ExpSelf("异常了,自己定义的...", new IOException());
}
} catch (ExpSelf e) {
//e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.fillInStackTrace());
System.out.println(e.toString());
} } }
05-02 18:29