package day05;
public class DemoTryCatch_1 {
public static void main(String[] args) {
try{
String str2 = "";
System.out.println(str2.charAt(1));
} catch(NumberFormatException e){
System.out.println("数据转换异常!");
} catch (NullPointerException e) {
System.out.println("空指针异常!");
} catch(Exception e){
System.out.println("反正就是出了个错!");
}
}
}
package day05;
public class DemoTryCath02_2 {
public static void main(String[] args) {
Object obj = "hello";
System.out.println((Integer)obj);
String str = "s";
System.out.println(Integer.parseInt(str));
}
}
package day05;
public class DemoFinally_3 {
public static void main(String[] args) {
System.out.println("程序开始执行:");
try {
String str = "shg";
System.out.println(str.charAt(2));
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("finally代码执行了:");
}
}
}
package day05;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FinallyDemo2_4 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("osw.txt");
byte[] bytes = new byte[100];
int len = fis.read(bytes);
System.out.println(new String(bytes,0,len,"UTF-8"));
} catch (Exception e) {
System.out.println("读取文件出现异常...");
e.printStackTrace();
} finally{
try {
if(fis!=null){
System.out.println("关闭流,释放资源");
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package day05;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class DemoThrows_5 {
public static void testRead() throws FileNotFoundException{
FileInputStream fis = new FileInputStream("pw2.txt");
}
public static void test() throws FileNotFoundException{
testRead();
}
public static void main(String[] args) {
try {
test();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
package day05;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class DemoTryCatch_6 {
public static void main(String[] args) {
try(PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("ceshi.txt"))));) {
pw.println("祝愿全天下的母亲节日快乐!");
System.out.println("写入完毕!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package day05;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class DemoThrow_7 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("hhh.txt");
} catch (Exception e) {
throw new RuntimeException("读取失败,请联系相关人员...");
} finally {
try {
if(fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("程序结束!");
}
}
package day05;
import java.util.Scanner;
public class demoRuntimeException_8 {
public static void register(){
String name = "admin";
Scanner scanner = new Scanner(System.in);
String inputName = scanner.next();
if(name.equals(inputName)){
throw new RuntimeException("用户名被占用");
} else {
System.out.println("请继续填写:");
}
}
public static void main(String[] args) {
try {
register();
} catch (RuntimeException e) {
String excMessage = e.getMessage();
System.out.println(excMessage);
}
}
}
package day05;
public class DemoRuntimeException02_9 {
public static void register(){
String name = "admin";
if(name.equals("admin")){
throw new NameAlreadyException("姓名被占用!");
}
}
public static void main(String[] args) {
try {
register();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
}
package day05;
public class Person_11 {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException {
if(age<0 || age>120){
throw new AgeException("年龄不合法,请认真填写您的年龄!");
}
this.age = age;
}
@Override
public String toString() {
return "Person_11 [age=" + age + "]";
}
}
package day05;
public class DemoException02_12 {
public static void main(String[] args) {
Person_11 p = new Person_11();
try {
p.setAge(10000);
} catch (AgeException e) {
System.out.println(e.getMessage());
}
}
}
package day05;
public class DemoException03_13 {
public void test() throws Exception{
}
}
class Demo04 extends DemoException03_13{
public void test() throws NullPointerException{
}
}
package day05;
import java.awt.AWTException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
public class DemoException04_14 {
public void dosome() throws IOException,AWTException{
}
}
class Son extends DemoException04_14{
}