Abnormal record of learning java

Unusual classification

  1. They need treatment
try, catch, finally
  1. Let someone else deal with the
throws

Here Insert Picture Description

Practical examples

Test.java



public class Test {     
	public static void main (String  args[]) {              
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = div(m, n);
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) {
		int r = 0;
		r= m/n;
		return r;
	}

}

Implementation of java Test 9 0 will throw an exception count, how to capture it?


public class Test {     
	public static void main (String  args[]) {              
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = div(m, n);
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) {
		int r = 0;
		try {
			r = m/n;
		} catch (ArithmeticException e) {
			System.out.println(e);
		}

		return r;
	}

}


Results of the

lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0
java.lang.ArithmeticException: / by zero
m/n=0

finally is whether or not an exception occurs, will perform a final statement!

Plus finally look at the results!

	public static int div(int m, int n) {
		int r = 0;
		try {
			r = m/n;
		} catch (ArithmeticException e) {
			System.out.println(e);
		} finally {
			System.out.println("this is finally!");
		}

		return r;
	}

The results of the implementation, 9/1 or 9/0 final statement will be executed finally

lydia@lydia:~/sgy/java_learn/exception$ javac Test.java
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 1
this is finally!
m/n=9
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0
java.lang.ArithmeticException: / by zero
this is finally!
m/n=0

How to let others deal

For example div function itself does not handle, throw the main method


public class Test {     
	public static void main (String  args[]) {              
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = 0;
		try {
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println(e);
		}
			
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		r = m / n;
		return r;
	}

}

main div and how to deal with this anomaly?

Now the program will only handle the exception in a div!
Test.java


public class Test {     
	public static void main (String  args[]) {              
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = 0;
		try {
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main-->"+e);
		}
			
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
		}
		
		return r;
	}

}


Implementation of the results will handle this exception only div

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
m/n=0

Need to manually throw an exception!

The revised code!

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw e;
		}
		
		return r;
	}

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
main-->java.lang.ArithmeticException: / by zero
m/n=0

Abnormal too much, no one listed, capturing the parent class exception, or an exception to categories

Test.java


public class Test {     
	public static void main (String  args[]) {              
		int m = 0;		
		int n =0;

		int r = 0;
		try {
			m = Integer.parseInt(args[0]);
			n = Integer.parseInt(args[1]);
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main-->"+e);
		} catch (RuntimeException e) {
			System.out.println("main-->"+e);
		}
			
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw e;
		}
		
		return r;
	}

}


Results of the

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
main-->java.lang.ArithmeticException: / by zero
m/n=0
lydia@lydia:~/sgy/java_learn/exception$ java Test 9  a
main-->java.lang.NumberFormatException: For input string: "a"
m/n=0
lydia@lydia:~/sgy/java_learn/exception$ java Test 9
main-->java.lang.ArrayIndexOutOfBoundsException: 1
m/n=0

Artificial new execption

Test.java
div function into this below!

	public static int div(int m, int n)  {
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw new Exception("My error!");
		}
		
		return r;
	}

Compiler does not pass, new Execption must deal with or throw!

lydia@lydia:~/sgy/java_learn/exception$ javac Test.java
Test.java:27: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出
                        throw new Exception("My error!");
                        ^
1 个错误

Continues to change, div function

	public static int div(int m, int n) throws Exception{
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw new Exception("My error!");
		}
		
		return r;
	}

Compile result is still the same, Main function which also need to be addressed!

lydia@lydia:~/sgy/java_learn/exception$ javac Test.java
Test.java:11: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出
                        r = div(m, n);
                               ^
1 个错误

Main method of increasing the catch Exception, handle the exception, there is no problem


public class Test {     
	public static void main (String  args[]) {              
		int m = 0;		
		int n =0;

		int r = 0;
		try {
			m = Integer.parseInt(args[0]);
			n = Integer.parseInt(args[1]);
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main-->"+e);
		} catch (RuntimeException e) {
			System.out.println("main-->"+e);
		} catch (Exception e) {
			System.out.println("main-->"+e);
		}
			
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) throws Exception{
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw new Exception("My error!");
		}
		
		return r;
	}

}

Implementation of the results

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
main-->java.lang.Exception: My error!
m/n=0

Or dispose of them directly in the div!
Increase the try catch statement in a div, direct approach to this!

	public static int div(int m, int n) throws Exception{
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			try {
				throw new Exception("My error!");
			} catch (Exception e1) {
				System.out.println("div-->"+e1);
			}
			
		}
		
		return r;
	}
lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
div-->java.lang.Exception: My error!
m/n=0

PS:

  1. or try with a return catch block or throw statement, first performs finally block, then return to the execution of the return or throw statement

  2. For "non-search exception", the system can also throw it, do not write the same effect as writing throws
    That throws ArithmeticException not write, main methods are still able to properly capture! Automatically throw

	public static int div(int m, int n) throws ArithmeticException {

Implementation of the results

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
main-->java.lang.ArithmeticException: / by zero
m/n=0
Published 17 original articles · won praise 3 · Views 3474

Guess you like

Origin blog.csdn.net/sgy1993/article/details/104573049