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 异常的更多相关文章

  1. JAVA自学笔记23

    JAVA自学笔记23 1.多线程 1)引入: 2)进程 是正在运行的程序.是系统进行资源分配和调用的独立单位.每一个进程都有它自己的内存空间和系统资源. 多进程: 单进程的计算机只能做一件事情,而现在 ...

  2. 1.13(java学习笔记)异常机制

    异常不同于错误,它是程序运行时产生的未知问题. 如果把程序比喻成一辆汽车,那么汽车开着开着突然前面出现了一个大石头挡住了路,这就叫异常. 那么出现了这个异常我们需要去处理,比如打电话给公路管理局,让它 ...

  3. java新手笔记32 jdk5新特性

    1.for package com.yfs.javase; import java.awt.Color; import java.util.Calendar; import java.util.Has ...

  4. java新手笔记31 集合实现类

    Person类: package com.yfs.javase; import java.util.Date; public class Person implements Comparable { ...

  5. java新手笔记4 数组

    1.数组 import java.util.Random; public class ArrayDemo1 { public static void main(String[] args) { int ...

  6. java新手笔记1 Hello World!

    //Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { S ...

  7. 【原】Java学习笔记030 - 异常

    package cn.temptation; public class Sample01 { public static void main(String[] args) { /* * 异常:Java ...

  8. Java学习笔记__异常机制_try_catch_finally_return执行顺序

    package cn.xiaocangtian.Exception; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...

  9. JAVA新手笔记 Intent对象和Bundle对象

    Intent对象和Bundle对象 功能主要是在 MainActivity中定义了2个EditText,当用户输入内容,把他传入到第二个活动, 自己新创的活动中,MyActivity中 放在MainA ...

随机推荐

  1. HW输入字符串长度,字符串,计数m。从前往后计数,当数到m个元素时,m个元素出列,同时将该元素赋值给m,然后从下一个数计数循环,直到所有数字都出列,给定的数全部为大于0的数字。输出出队队列。

    package huawei; import java.util.Scanner; public class 约瑟夫环 { private static class Node { public int ...

  2. JDK1.5新特性(二)……Static Import

    援引 Static Import - This facility lets you avoid qualifying static members with class names without t ...

  3. HW1.4

    public class Solution { public static void main(String[] args) { System.out.println("a a^2 a^3& ...

  4. 51nod1009(1的数目)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009 题意:中文题诶- 思路:分别考虑各个数位上出现1的次数 ...

  5. Oracle- 初识

    我一直没用过ORACLE.今天总算装上了,说一下我装的过程,感觉还是有点折腾的. 一.我装的是ORACLE 9版本,从网上下载的总大小1.3G.是三个压缩包. 首先我解压三个压缩包后,使用UltraI ...

  6. NSUserDefaults偶尔/有时候保存数据会失败/失效

    之前已经实现了通过NSUserDefaults去保存用户数据: [已解决]iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArra ...

  7. cocos2dx js文件加密为jsc文件

    发布产品,脚本代码是必须要加密的 偶尔会出现编译后的jsc无法运行,或者某些jsb自定义的函数找不到, 最好将require("jsb.js")的全部内容整合到一个文件,然后编译j ...

  8. 标准I/O库之格式化I/O

    本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. 一.格式化输出 执行格式化输出处理的是4个pr ...

  9. 非spring环境中配置文件工具

    http://commons.apache.org/proper/commons-configuration/ 注意:属性的值使用","分割,会造成读取属性值不正确的问题.建议使用 ...

  10. Using Sessions and Session Persistence---reference

    Using Sessions and Session Persistence The following sections describe how to set up and use session ...