java基础学习之异常Exception 十
异常Exception
package exception;
import java.io.FileInputStream;
/**
* 异常 : 程序出现了不正常的情况
* @author Angus
*
* 我们在编写程序的时候,可能有很多问题存在,为了将来方便的表示浙西额的问题原因,类型,位置,java就提供了异常对象供我们使用
*/
public class ExceptionDemo {
public static void main(String[] args) {
int a = 10;
int b = 0;
System.out.println(a / b);
//Exception in thread "main" java.lang.ArithmeticException: / by zero
// FileInputStream fis = new FileInputStream("a.txt");
}
}
类 Throwable
Throwable是java.lang包中一个专门用来处理异常的类。它有两个子类,即Error和Exception,它们分别用来处理两组异常
类 Throwable
* Error:严重问题, 需要修改代码或者设备支持。。
* Exception:不严重
* 编译期间:需要我们自己处理
* 运行期间:不需要处理,需要修改代码或者传递参数
package exception;
import java.io.FileInputStream;
import java.util.Scanner;
/**
* @author Angus
* 出现怎么解决呢?
*
* 如果我们没有处理,java虚拟机在会控制台打印错误。。
*
* A基本格式:
* try{
* 可能发生问题的代码
* }catch(异常类名 变量名){
* 异常处理
* }
* B 抛出
*
*/
public class ExceptionDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据");
int a = sc.nextInt();
int b = sc.nextInt();
try{
int c = a/b ;
System.out.println(c);
}catch(Exception e){
System.out.println(e); //异常输出
}
}
}
Throwable方法使用
package exception;
import java.io.FileInputStream;
import java.util.Scanner;
/**
* @author Angus
* Throwable
*
* getMessage() 返回此 throwable 的详细消息字符串。
* toString() 返回此 throwable 的简短描述。
* printStackTrace() 将此 throwable 及其追踪输出至标准错误流
*/
public class ExceptionDemo {
public static void main(String[] args) {
int a = 10;
int b = 0;
try{
System.out.println(a /b );
}catch(Exception e){
// System.out.println(e);
System.out.println(e.getMessage()); // / by zero
System.out.println(e.toString()); // java.lang.ArithmeticException: / by zero
e.printStackTrace();
}
}
}
finally简单使用:
package exception;
/**
*
* @author Angus
* 基本格式
* try{
* 有问题代码
* }catch{
* 处理方案
* }finally{
* 永远执行。。。
* }
*/
public class FinallyDemo {
public static void main(String[] args) {
int a = 10;
int b = 0;
try{
System.out.println(a /b);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("over");
}
}
}
结果:
测试:常见面试题
package exception;
/**
*
* @author Angus
*
* 1;final,fianlly,finalize 区别?
* 2:finally会永远执行吗?
* 会,但是有一个特殊情况,在执行到finally之前JVM退出。。。
* 3:在catch中加入return finally代码是否执行?
* 会执行,在return之前执行
* 准确说在return之间。。。
*/
public class FinallyDemo {
public static void main(String[] args) {
// method1();
// method2();
int result = method3();
System.out.println(result);
}
private static int method3() {
int a = 10;
int b = 0;
try{
System.out.println(a /b);
System.out.println("a1.."+a);
}catch(Exception e){
// e.printStackTrace();
a = 20;
System.out.println("a2.."+a);
return a;
}finally{
System.out.println("over");
System.out.println("a3..."+a);
a = 30;
}
return a;
}
private static void method2() {
int a = 10;
int b = 0;
try{
System.out.println(a /b);
}catch(Exception e){
e.printStackTrace();
return; //在catch中加入return finally代码是否执行?
}finally{
System.out.println("over");
}
}
private static void method1() {
int a = 10;
int b = 0;
try{
System.out.println(a /b);
}catch(Exception e){
e.printStackTrace();
System.exit(0); //finally会永远执行吗?
}finally{
System.out.println("over");
}
}
}
可以自己运行代码查看结果。。。。
抛出异常
package exception;
import java.io.FileInputStream;
import java.util.Scanner;
/**
* @author Angus
* 抛出异常
* 格式;
* 在方法名称后跟关键字: throws 异常类名
*
* 异常的分类:
* A:Exception 编译时异常
* B:RunTimeException 运行时异常
*/
public class ExceptionDemo {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//告诉你这个方法可能有问题,需要注意。。但是没有处理。。
// private static void method() throws ArithmeticException{
// int a = 10;
// int b = 0;
// System.out.println(a /b );
// }
private static void method() throws Exception{
int a = 10;
int b = 0;
System.out.println(a /b );
}
}
还没有评论,来说两句吧...