做网站的公司怎么推销,平面设计网站有哪些比较好,h5教程入门,公司网站如何建设目录
一、什么是异常
二、异常抛出机制
三、如何对待异常
四、 Java异常体系
4.1、Throwable
4.2、Error 4.2、Exception
4.2.1、编译时异常
4.2.2、运行时期异常
五、异常处理
5.1、捕获异常#xff08;try-catch#xff09;
5.1.2、catch中异常处理方式 …目录
一、什么是异常
二、异常抛出机制
三、如何对待异常
四、 Java异常体系
4.1、Throwable
4.2、Error 4.2、Exception
4.2.1、编译时异常
4.2.2、运行时期异常
五、异常处理
5.1、捕获异常try-catch
5.1.2、catch中异常处理方式 5.2、finally使用
5.3、声明抛出异常类型throws
六、手动抛出异常 七、自定义异常 一、什么是异常
指的是程序在执行过程中出现的非正常情况如果不处理最终会导致JVM的非正常停止。异常指的并不是语法错误和逻辑错误。语法错了编译不通过不会产生字节码文件根本不能运行。代码逻辑错误只是没有得到想要的结果。
二、异常抛出机制
Java中把不同的异常用不同的类表示一旦发生某种异常就创建该异常类型的对象并且抛出throw。然后程序员可以捕获(catch)到这个异常对象并处理如果没有捕获(catch)这个异常对象那么这个异常对象将会导致程序终止。
三、如何对待异常
对于程序出现的异常一般有两种解决方法一是遇到错误就终止程序的运行。另一种方法是程序员在编写程序时就充分考虑到各种可能发生的异常和错误极力预防和避免。实在无法避免的要编写相应的代码进行异常的检测、以及异常的处理保证代码的健壮性。
四、 Java异常体系
4.1、Throwable
java.lang.Throwable类是Java程序执行过程中发生的异常事件对应的类的根父类
4.2、Error
Java虚拟机无法解决的严重问题。如JVM系统内部错误、资源耗尽等严重情况。一般不编写针对性的代码进行处理。
public class ErrorTest {public static void main(String[] args) {//StackOverflowError:栈溢出错误main(args);}
} public class ErrorTest {public static void main(String[] args) {//StackOverflowError:栈溢出错误//main(args);//OutOfMemoryError:堆空间溢出byte[] bytes new byte[1024 * 1024 * 100];//100MB}
} 4.2、Exception
其它因编程错误或偶然的外在因素导致的一般性问题需要使用针对性的代码进行处理使程序继续运行。否则一旦发生异常程序也会挂掉。
4.2.1、编译时异常
即checked异常、受检异常在代码编译阶段编译器就能明确警示当前代码可能发生不是一定发生xx异常并明确督促程序员提前编写处理它的代码。如果程序员没有编写对应的异常处理代码则编译器就会直接判定编译失败从而不能生成字节码文件。通常这类异常的发生不是由程序员的代码引起的或者不是靠加简单判断就可以避免的例如FileNotFoundException文件找不到异常。
Testpublic void test1(){//ClassNotFoundException//Class.forName(java.lang.String);}
4.2.2、运行时期异常 即runtime异常、unchecked异常、非受检异常在代码编译阶段编译器完全不做任何检查无论该异常是否会发生编译器都不给出任何提示。只有等代码运行起来并确实发生了xx异常它才能被发现。通常这类异常是由程序员的代码编写不当引起的只要稍加判断或者细心检查就可以避免。
public class ExceptionTest {Testpublic void test1(){//ArrayIndexOutOfBoundsExceptionint[] arr new int[5];System.out.println(arr[5]);}Testpublic void test2(){//NullPointerExceptionString name null;System.out.println(name.toString());}Testpublic void test3(){//ClassCastExceptionObject o new String();Date date (Date) o;}Testpublic void test4(){//NumberFormatExceptionString str abc;int i Integer.parseInt(str);}Testpublic void test5(){//InputMismatchExceptionScanner scanner new Scanner(System.in);int num scanner.nextInt();System.out.println(num);}Testpublic void test6(){//ArithmeticExceptionint num 3;System.out.println(num / 0);}
}五、异常处理
5.1、捕获异常try-catch Testpublic void test1(){try {int num 3;System.out.println(num / 0);} catch (ArithmeticException e) {System.out.println(处理算数异常);}catch (NullPointerException e){System.out.println(处理空指针异常);}catch (RuntimeException e){System.out.println(处理运行时异常);}System.out.println(异常处理完成程序继续执行);} 注意catch结构中的异常类型出现父子关系父类必须在子类下面否则报错 5.1.2、catch中异常处理方式
三种1、自己编写输出语句。2、printStackTrace()打印异常的详细信息。3、getMessage()获取发生异常的原因。
Testpublic void test2(){try {int num 3;System.out.println(num / 0);} catch (ArithmeticException e) {e.printStackTrace();}catch (NullPointerException e){System.out.println(处理空指针异常);}catch (RuntimeException e){System.out.println(e.getMessage());}System.out.println(异常处理完成程序继续执行);} 5.2、finally使用 Testpublic void test1(){try {int num 3;System.out.println(num / 0);}finally {System.out.println(异常处理完成程序继续执行);}} 一定要放在finally中主要代码使用的流或其它需要关闭的资源防止内存泄漏。 5.3、声明抛出异常类型throws
Testpublic void test2() throws ArithmeticException{int num 3;System.out.println(num / 0);System.out.println(异常处理完成程序继续执行);}
六、手动抛出异常
Testpublic void test3() {int num1 3;int num2 0;if (num2 0){System.out.println(num1 / num2);}else {throw new ArithmeticException(除数必须大于0);}} 七、自定义异常
class MyException extends Exception {static final long serialVersionUID 23423423435L;private int idnumber;public MyException(String message, int id) {super(message);this.idnumber id;}public int getId() {return idnumber;}
}