前言
一、异常
算数异常,数组越界异常,空指针异常等等。
Exception(异常)又分为受查异常(编译时期异常)(如果不处理程序都不能够编译)和非受查异常(运行时期异常)(RunTimeException及其子类)(运行后显示的红色异常信息提示)。
Error必须程序员处理,如栈溢出错误,是逻辑上的错误。
二、异常的处理
一般用事后认错型异常
throw与throws异常的抛出
throw抛出一个指定的异常。后期一般会通过它抛出一个自定义类型的异常。
public static void main(String[] args) {
int a=10;
if(a==10){
throw new NullPointerException();
}
}
当方法抛出编译异常,用户不象处理该异常,就可以用throws将异常抛给方法的调用者来处理。(让代码不报错,可以运行)(参考前面的克隆抛出异常)(在java抽象类和接口中有详细讲解)
throw是抛出一个对象,throws是在方法中声明有可能会抛出一个对象。
注意:throw:
throws:
try-catch/捕获异常并处理
try{
System.out.println(10/0);
System.out.println("不会执行这个");//注意异常后的这个不执行
}catch (ArithmeticException e){
System.out.println("我捕获了这个异常");
}
System.out.println("after");//执行
}
如果异常捕获不对,就会显示如下:
try{
System.out.println(10/0);
}catch (NullPointerException e){
System.out.println("我捕获了这个异常");
}
System.out.println("after");
}
注意:不可能同时捕获多个异常,就算有多个(可以catch多个,因为有时不知道什么异常),也只能捕获到一个然后运行。不建议在catch中放多个异常用|分开。
try块内抛出异常之后的代码将不会被执行。
从上往下,先捕捉子类异常,然后再捕捉父类异常。
但是最好不要直接使用Exception抛出异常,因为抛出的异常不知道是什么类型。
finally
不管是否发生异常,finally中的内容终将被执行。
当try和finally中都有return时只取finally中return。finally中代码一定会被执行。
异常的处理流程
e.printStackTrace();
方一
public class Test01 {
public static void func(int a){
try{
System.out.println(10/a);
System.out.println("不会执行这个");
}catch (ArithmeticException e){
e.printStackTrace();
System.out.println("我捕获了这个异常");
}
}
public static void main(String[] args) {
func(0);
}
}
方二:在main函数中处理异常
public class Test01 {
public static void func(int a){
System.out.println(10/a);
}
public static void main(String[] args) {
try{
func(0);
System.out.println("不会执行这个");
}catch (ArithmeticException e){
// e.printStackTrace();
System.out.println("我捕获了这个异常");
}
}
}
在方法的调用过程中,如果被调用的方法没有处理异常,会向上把这个异常抛给调用者去处理。
调用者也没有处理,就会抛给JVM。
自定义异常
继承与Exception(受查) 和RunTimeException.(非受查)
运行时异常
当输入密码不对抛出异常
class Login{
private String userName="1234";
private String password="12345";
public void loginIn(String userName,String password){
if(!this.userName.equals(userName)){
throw new UserNameException();
}
if(!this.password.equals(password)){
throw new PasswordException();
}
System.out.println("登陆成功");
}
}
public class Test01 {
public static void main(String[] args) {
Login login=new Login();
login.loginIn("123456","1234567");
}
}
public class UserNameException extends RuntimeException{
}
public class PasswordException extends RuntimeException{
}
这是运行时异常,异常继承的是RuntimeException.
要使运行不报红,可以用try-catch抛出。
编译时异常,继承Exception。但是必须得要try-catch等处理才可运行。
编译时异常
class Login{
private String userName="1234";
private String password="12345";
public void loginIn(String userName,String password)throws UserNameException,PasswordException{
if(!this.userName.equals(userName)){
throw new UserNameException(userName+"用户名错误");
}
if(!this.password.equals(password)){
throw new PasswordException(password+"密码错误");
}
System.out.println("登陆成功");
}
}
public class Test01 {
public static void main(String[] args) {
Login login=new Login();
try{
login.loginIn("1234","1234567");}
catch (UserNameException e){
e.printStackTrace();
}
catch (PasswordException e){
e.printStackTrace();
}
}
}
public class UserNameException extends RuntimeException{
public UserNameException() {
}
public UserNameException(String message) {
super(message);
}
}
public class PasswordException extends Exception{
public PasswordException() {
}
public PasswordException(String message) {
super(message);
}
}
当用户名密码错误
当用户名密码正确
总结
如何衡量一个代码的好与坏。1.时间复杂度空间复杂度2.可读性高。
javaSE语法完毕!