Java . Java , , - . , Exception. , . , .
Java 5 : try, catch, throw, throws finally. . (try) , , (throw) , (catch) (finally) .
.
try {
// }
catch (1 ) {
// 1 }
catch (2 ) {
// 2
throw(e) // }
finally {
}
Delphi catch except.
Throwable. Throwable. Throwable . ception , . Throwable Error, , .
- Java . , , .
class Exc0 {
public static void main(string args[]) {
int d = 0;
int a = 42 / d;
} }
, .
:\> java Exc0
java.lang.ArithmeticException: / by zero
at Exc0.main(Exc0.java:4)
Exception Throwable. Exception, : ArithmeticException, , . , , main.
class Exc1 {
static void subroutine() {
int d = 0;
int a = 10 / d;
}
public static void main(String args[]) {
Exc1.subroutine();
} }
, Java .
:\> java Exc1
java.lang.ArithmeticException: / by zero
at Exc1.subroutine(Exc1.java:4)
at Exc1.main(Exc1.java:7)
try catch
, , try. try- catch, .
class Exc2 {
public static void main(String args[]) {
try {
int d = 0;
int a = 42 / d;
}
catch (ArithmeticException e) {
System.out.println("division by zero");
}
} }
catch- , , ( division by zero).
catch
. , , Java catch- try-. , , . , catch , Throwable.
class MultiCatch {
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
}
catch (ArithmeticException e) {
System.out.println("div by 0: " + e);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("array index oob: " + e);
}
} }
, , . , , , ArrayIndexOutOf Bounds. , .
:\> java MultiCatch
= 0
div by 0: java.lang.ArithmeticException: / by zero
array index oob: java.lang.ArrayIndexOutOfBoundsException: 42
try , . try catch, , , catch try. , try .
class MultiNest {
static void procedure() {
try {
int c[] = { 1 };
c[42] = 99;
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("array index oob: " + e);
} }
public static void main(String args[]) {
try {
int a = args.length();
System.out.println("a = " + a);
int b = 42 / a;
procedure();
}
catch (ArithmeticException e) {
System.out.println("div by 0: " + e);
}
} }
throw
throw . , , Throwable, catch, new. throw.
throw Throwable;
, . try catch. , . , try, catch, Java , . , -, throw , catch.
class ThrowDemo {
static void demoproc() {
try {
throw new NullPointerException("demo");
}
catch (NullPointerException e) {
System.out.println("caught inside demoproc");
throw e;
} }
public static void main(String args[]) {
try {
demoproc();
}
catch(NulPointerException e) {
System.out.println("recaught: " + e);
}
} }
. main demoproc. demoproc , NullPointerException throw . demoproc, - e. , , throw, main. , .
caught inside demoproc
recaught: java.lang.NullPointerException: demo
throws
, , , . , , throws. (.. throw) , throws . :
_( ) throws _ {}
, procedure , , . .
class ThrowsDemo1 {
static void procedure() {
System.out.println("inside procedure");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
procedure();
} }
, , , procedure IllegalAccessException main :
class ThrowsDemo {
static void procedure() throws IllegalAccessException {
System.out.println(" inside procedure");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
procedure();
}
catch (IllegalAccessException e) {
System.out.println("caught " + e);
}
} }
.
inside procedure
caught java.lang.IllegalAccessException: demo
finally
, , . finally. , catch, finally , , try. try catch finally. finally , . , , finally.
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");
} }
public static void main(String args[]) {
try {
procA();
}
catch (Exception e) {}
procB();
} }
procA - try, finally. procB try- return, finally. , .
:\> java FinallyDemo
inside procA
procA's finally
inside procB
procB's finally
Throwable . int, char .., , Throwable, , String Object, . Exception. , Exception.
class MyException extends Exception {
private int detail;
MyException(int a) {
detail = a:
}
public String toString() {
return "MyException[" + detail + "]";
}
}
class ExceptionDemo {
static void compute(int a) throws MyException {
System.out.println("called computer + a + ").");
if (a > 10)
throw new MyException(a);
System.out.println("normal exit.");
}
public static void main(String args[]) {
try {
compute(1);
compute(20);
}
catch (MyException e) {
System.out.println("caught" + e);
}
} }
. MyException Exception. , , toString, , -. ExceptionDemo compute, MyExcepton. compute , - 10. main compute , ( 10), . .
:\> java ExceptionDemo
called compute(1).
normal exit.
called compute(20).
caught MyException[20]
. Try, throw, catch . , , , , .
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |