Exception
An exception is a run-time error.
Any code that absolutely must be executed after a try block completes is put in finally block.
package main;
public class Main {
public static void main(String[] args) {
int d, a;
try { // monitor a block of code
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
} catch (ArithmeticException e) { // catch divide-by-zero error
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}Output:
Division by zero.
After catch statement.
println() inside the try block is never executed. Once an exception is thrown, program
control transfers out of the try block into the catch block.
Throwable overrides the toString() method(defined by Object) so that it returns a stringcontaining a description of the exception.
} catch (ArithmeticException e) {
System.out.println(e);
a = 0;
}When you use multiple catch statements, it is important to remember that exception subclasses
must come before any of their superclasses. This is because a catch statement that uses a
superclass will catch exceptions of that type plus any of its subclasses. Thus, a subclass
would never be reached if it came after its superclass.
package main;
public class Main {
public static void main(String[] args) {
try {
int a = 0;
int b = 42 / a;
} catch (Exception e) {
System.out.println("Generic Exception catch.");
}
/* This catch is never reached because
Arithmetic Exception is a subclass of Exception
catch (ArithmeticException e) {
System.out.println("This is never reached".);
}
*/
}
}Since ArithmeticException is a subclass of Exception, the first catch statement will handleall Exception-based errors, including ArithmeticException. This means that the second catchstatement will never execute.Nested try Statements.package main;
public class Main {
public static void main(String[] args) {
try {
int a = 1;
int b = 2 / a;
System.out.println("a = " + a);
try {
if (a == 1) a = a / (a - a);
if (a == 2) {
int c[] = {1};
c[2] = 99;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
} catch (ArithmeticException e) {
System.out.println(e);
}
}
}Another example:
package main;
public class Main {
public static void main(String[] args) {
try {
try {
throw new NullPointerException("demo");
} catch (NullPointerException e) {
System.out.println("Caught inside");
throw e;
}
} catch (NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}Caught inside Recaught: java.lang.NullPointerException: demo
Throw.
package main;
public class ThrowDemo {
static void demoproc() {
try {
throw new NullPointerException("demo");
} catch (NullPointerException e) {
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String[] args) {
try {
demoproc();
} catch (NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}Caught inside demoproc. Recaught: java.lang.NullPointerException: demo
finally
finally creates a block of code that will be executed after a try/catch block has completed
and and before the code following the try/catch block. The finally block will execute wheter
or not an exception is thrown. If an exception is thrown, the finally block will execute
even if no catch statement matches the exception.
package main;
public class FinallyDemo {
static void procA() {
try {
System.out.println("inside procA");
throw new RuntimeException("demo");
} finally {
System.out.println("procA's finally");
}
}
static void procB() {
try {
System.out.println("inside procB");
return;
} finally {
System.out.println("procB's finally");
}
}
static void procC() {
try {
System.out.println("inside procC");
} finally {
System.out.println("procC's finally");
}
}
public static void main(String[] args) {
try {
procA();
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
procB();
procC();
}
}In this example, procA() prematurely breaks out of the try by throwing an exception. The
finally clause is executed on the way out. procB() 's try statement is exited via a return
statement. The finally clause is executed before procB() returns. In procC(), the try statement
executes normally, without error. However, the finally block is still executed.
package main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
try {
System.out.println(a / b);
}catch (Exception e){
e.printStackTrace();
System.out.println("Something went wrong");
} finally {
System.out.println("Finally part");
}
System.out.println("Continue...");
}
}
5 0 java.lang.ArithmeticException: / by zero at main.Main.main(Main.java:11) Something went wrong Finally part Continue...
Multiple catch exception
package main;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try {
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a / b);
} catch (ArithmeticException e1) {
System.out.println("Arithmetic exception");
} catch (InputMismatchException e2) {
System.out.println("Input mismatch exception");
} catch (Exception e3) {
System.out.println("Any type exception");
}
System.out.println("Continue...");
}
}
Creating our own Exception class.
package main;
public class MyException extends Exception {
public MyException() {
super("Some message...");
}
}package main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int age = in.nextInt();
try {
if (age < 18) throw new MyException();
} catch (MyException ex) {
ex.printStackTrace();
} finally {
System.out.println("finally");
}
System.out.println("continue...");
}
}Output:
1 main.MyException: Some message... at main.Main.main(Main.java:10) finally continue...
package main;
public class MyException extends Exception {
private int detail;
public MyException(int a) {
detail = a;
}
public String toString() {
return "MyException[" + detail + "]";
}
}package main;
public class Main {
public static void main(String[] args) {
try {
compute(1);
compute(20);
} catch (MyException e) {
System.out.println("Caught " + e);
}
}
public static void compute(int a) throws MyException {
System.out.println("Called compute(" + a + ")");
if (a > 10)
throw new MyException(a);
System.out.println("Normal exit");
}
}Output:
Called compute(1) Normal exit Called compute(20) Caught MyException[20]
Checked exception forces us to use try-catch block or throws. Checked exceptions generallyextends Exception.Unchecked exception does not force us to do so. Unchecked exceptions generally extendsRuntimeException.
Комментарии
Отправить комментарий