java新手笔记23 异常
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());
} } }
java新手笔记23 异常的更多相关文章
- JAVA自学笔记23
JAVA自学笔记23 1.多线程 1)引入: 2)进程 是正在运行的程序.是系统进行资源分配和调用的独立单位.每一个进程都有它自己的内存空间和系统资源. 多进程: 单进程的计算机只能做一件事情,而现在 ...
- 1.13(java学习笔记)异常机制
异常不同于错误,它是程序运行时产生的未知问题. 如果把程序比喻成一辆汽车,那么汽车开着开着突然前面出现了一个大石头挡住了路,这就叫异常. 那么出现了这个异常我们需要去处理,比如打电话给公路管理局,让它 ...
- java新手笔记32 jdk5新特性
1.for package com.yfs.javase; import java.awt.Color; import java.util.Calendar; import java.util.Has ...
- java新手笔记31 集合实现类
Person类: package com.yfs.javase; import java.util.Date; public class Person implements Comparable { ...
- java新手笔记4 数组
1.数组 import java.util.Random; public class ArrayDemo1 { public static void main(String[] args) { int ...
- java新手笔记1 Hello World!
//Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { S ...
- 【原】Java学习笔记030 - 异常
package cn.temptation; public class Sample01 { public static void main(String[] args) { /* * 异常:Java ...
- Java学习笔记__异常机制_try_catch_finally_return执行顺序
package cn.xiaocangtian.Exception; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- JAVA新手笔记 Intent对象和Bundle对象
Intent对象和Bundle对象 功能主要是在 MainActivity中定义了2个EditText,当用户输入内容,把他传入到第二个活动, 自己新创的活动中,MyActivity中 放在MainA ...
随机推荐
- BestCoder Round #81 (div.1)A
水题...就是n的三进制后m位 #include<cstdio> #include<cstring> #include<cstdlib> #include<i ...
- Hadoop系列(一)hadoop2.2.0源码编译
操作系统:Centos 所需条件:能上网 编译所需工具: apache-ant-1.9.2-bin.tar.gz apache-maven-3.0.5-bin.tar.gz findbugs-2.0. ...
- json里的日期字符串 怎么 转换成 javascript 的 Date 对象?
“/Date(1232035200000)/” 怎么转换成 javascript 的 Date 对象 做法:new Date(+/\d+/.exec(value)[1]); value就是json字 ...
- 用g++ 编译 ffmpeg 编译出现 error: 'UINT64_C' was not declared in this scope 或 missing -D__STDC_CONSTANT_MACROS
在 libavutil/common.h 下 添加如下,即可解决 #ifdef __cplusplus#define __STDC_CONSTANT_MACROS#ifdef _STDINT_H#un ...
- pm 2.5
定陵</a></div><div class="staaqi"><span class="label pmsmall" ...
- android图片闪烁或帧动画
remote_recording_transition.xml 文件 <?xml version="1.0" encoding="utf-8"?> ...
- 使用sessionStorage得一个坑
1.首先sessionStorage.setItem("isMybill" false) 2.使用的时候 sessionStorage.getItem('isMybill') / ...
- PS Studio打包程序 .net版本依赖
PS Studio打包好的程序(Win7下),拿到Windows Server 2003(PS2.0),如果提示”.Net framework初始化错误“,则需要安装 .net 3.0 如果是PS3. ...
- HDU 1847 Good Luck in CET-4 Everybody! (博弈论sg)
Good Luck in CET-4 Everybody! Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?或许紧张得连短学期的ACM都没工夫练习了.反正我知 ...
- Spring AOP切面
在软件开发中,分布于应用多出的功能被称为和横切关注点. 通常,这些横切关注点从概念上是与应用的业务逻辑相分离的(可是往往直接嵌入到应用的业务逻辑中).将这些横切关注点与业务逻辑相分离正是面向切面编成( ...