Исключения. Классы исключений

Содержание

Слайд 2

VI. Исключения 2. Классы исключений

VI. Исключения
2. Классы исключений


Слайд 3


Слайд 4

Класс Throwable public class Throwable implements Serializable { private String detailMessage;

Класс Throwable
public class Throwable implements Serializable {
private String detailMessage;
public Throwable()

{
fillInStackTrace();
}
public Throwable(String message) {
fillInStackTrace();
detailMessage = message;
}
public String getMessage() {
return detailMessage;
}
public String getLocalizedMessage() {
return getMessage();
}
public String toString() {
String s = getClass().getName();
String message = getLocalizedMessage();
return (message != null) ? (s + ": " + message) : s;
}
}
Слайд 5

Получение информации об исключении public class ExceptionMethodsDemo { public static void

Получение информации об исключении

public class ExceptionMethodsDemo {
public static void main(String[]

args) {
try {
throw new Exception("My Exception");
} catch (Exception e) {
System.out.println("Caught Exception");
System.out.println("getMessage():" + e.getMessage());
System.out.println("getLocalizedMessage():"
+ e.getLocalizedMessage());
System.out.println("toString():" + e);
}
}
}
Caught Exception
getMessage():My Exception
getLocalizedMessage():My Exception
toString():java.lang.Exception: My Exception
Слайд 6

Стек вызовов


Стек вызовов

Слайд 7


Слайд 8

Класс Throwable public class Throwable implements Serializable { private StackTraceElement[] stackTrace;

Класс Throwable

public class Throwable implements Serializable {
private StackTraceElement[] stackTrace;
public

void printStackTrace() {
printStackTrace(System.err);
}
public void printStackTrace(PrintStream s) {
synchronized (s) {
s.println(this);
StackTraceElement[] trace = getOurStackTrace();
for (int i=0; i < trace.length; i++)
s.println("\tat " + trace[i]);
Throwable ourCause = getCause();
if (ourCause != null)
ourCause.printStackTraceAsCause(s, trace);
}
}
public StackTraceElement[] getStackTrace() {
return (StackTraceElement[]) getOurStackTrace().clone();
}
private synchronized StackTraceElement[] getOurStackTrace() {
if (stackTrace == null) {
int depth = getStackTraceDepth();
stackTrace = new StackTraceElement[depth];
for (int i=0; i < depth; i++)
stackTrace[i] = getStackTraceElement(i);
}
return stackTrace;
}
native int getStackTraceDepth();
}
Слайд 9

Стек вызовов public class StackTraceDemo { static void methodA() throws Exception

Стек вызовов

public class StackTraceDemo {
static void methodA() throws Exception {

throw new Exception();
}
static void methodB() throws Exception {
methodA();
}
static void methodC() throws Exception {
methodB();
}
...
}
Слайд 10

Стек вызовов public class StackTraceDemo { ... public static void main(String[]

Стек вызовов

public class StackTraceDemo {
...
public static void main(String[] args)

{
try {
methodA();
} catch (Exception e) {
for (StackTraceElement ste : e.getStackTrace())
System.out.println(ste.getMethodName());
}
System.out.println("--------------------------------");
try {
methodB();
} catch (Exception e) {
for (StackTraceElement ste : e.getStackTrace())
System.out.println(ste.getMethodName());
}
System.out.println("--------------------------------");
try {
methodC();
} catch (Exception e) {
for (StackTraceElement ste : e.getStackTrace())
System.out.println(ste.getMethodName());
}
}
}
Слайд 11

Стек вызовов methodA main -------------------------------- methodA methodB main -------------------------------- methodA methodB methodC main

Стек вызовов
methodA
main
--------------------------------
methodA
methodB
main
--------------------------------
methodA
methodB
methodC
main

Слайд 12

Стек вызовов public class AnotherStackTraceDemo { ... public static void main(String[]

Стек вызовов

public class AnotherStackTraceDemo {
...
public static void main(String[] args)

{
try {
methodA();
} catch (Exception e) {
e.printStackTrace(System.out);
}
System.out.println("--------------------------------");
try {
methodB();
} catch (Exception e) {
e.printStackTrace(System.out);
}
System.out.println("--------------------------------");
try {
methodC();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
Слайд 13

Стек вызовов java.lang.Exception at classes.AnotherStackTraceDemo.methodA(AnotherStackTraceDemo.java:6) at classes.AnotherStackTraceDemo.main(AnotherStackTraceDemo.java:19) -------------------------------- java.lang.Exception at classes.AnotherStackTraceDemo.methodA(AnotherStackTraceDemo.java:6)

Стек вызовов
java.lang.Exception
at classes.AnotherStackTraceDemo.methodA(AnotherStackTraceDemo.java:6)
at classes.AnotherStackTraceDemo.main(AnotherStackTraceDemo.java:19)
--------------------------------
java.lang.Exception
at classes.AnotherStackTraceDemo.methodA(AnotherStackTraceDemo.java:6)
at classes.AnotherStackTraceDemo.methodB(AnotherStackTraceDemo.java:10)
at classes.AnotherStackTraceDemo.main(AnotherStackTraceDemo.java:26)
--------------------------------
java.lang.Exception
at classes.AnotherStackTraceDemo.methodA(AnotherStackTraceDemo.java:6)
at classes.AnotherStackTraceDemo.methodB(AnotherStackTraceDemo.java:10)
at classes.AnotherStackTraceDemo.methodC(AnotherStackTraceDemo.java:14)
at classes.AnotherStackTraceDemo.main(AnotherStackTraceDemo.java:33)

Слайд 14

Элемент стека вызовов


Элемент стека вызовов

Слайд 15

Класс Throwable public class Throwable implements Serializable { public void setStackTrace(StackTraceElement[]

Класс Throwable

public class Throwable implements Serializable {
public void setStackTrace(StackTraceElement[] stackTrace)

{
StackTraceElement[] defensiveCopy =
(StackTraceElement[]) stackTrace.clone();
for (int i = 0; i < defensiveCopy.length; i++)
if (defensiveCopy[i] == null)
throw new NullPointerException("stackTrace[" + i + "]");
this.stackTrace = defensiveCopy;
}
}
Слайд 16

Класс StackTraceElement public class StackTraceElement implements java.io.Serializable { private String declaringClass;

Класс StackTraceElement

public class StackTraceElement implements java.io.Serializable {
private String declaringClass;

private String methodName;
private String fileName;
private int lineNumber;
public boolean isNativeMethod() { return lineNumber == -2; }
public StackTraceElement(String declaringClass, String methodName, String fileName, int lineNumber) {
if (declaringClass == null)
throw new NullPointerException("Declaring class is null");
if (methodName == null)
throw new NullPointerException("Method name is null");
this.declaringClass = declaringClass;
this.methodName = methodName;
this.fileName = fileName;
this.lineNumber = lineNumber;
}
public String toString() {
return getClassName() + "." + methodName +
(isNativeMethod() ? "(Native Method)" :
(fileName != null && lineNumber >= 0 ?
"(" + fileName + ":" + lineNumber + ")" :
(fileName != null ? "("+fileName+")" : "(Unknown Source)")));
}
...
}

C

Слайд 17

Элемент стека вызовов public class StackTraceElementDemo { public static void main(String[]

Элемент стека вызовов

public class StackTraceElementDemo {
public static void main(String[] args)

{
try {
methodA();
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void methodA() throws RuntimeException {
RuntimeException t = new RuntimeException("This is a new Exception...");
StackTraceElement[] trace = new StackTraceElement[] { new StackTraceElement(
"ClassName", "methodName", "fileName", 5) };
t.setStackTrace(trace);
throw t;
}
}
java.lang.RuntimeException: This is a new Exception...
at ClassName.methodName(fileName:5)
Слайд 18

Перебрасывание исключений из блока catch


Перебрасывание исключений из блока catch

Слайд 19


Слайд 20

Перебрасывание исключений public class RethrowingDemo { public static void methodA() throws

Перебрасывание исключений

public class RethrowingDemo {
public static void methodA() throws Exception

{
System.out.println("Originating the exception in methodA()");
throw new Exception("Thrown from methodA()");
}
public static void methodB() throws Exception {
try {
methodA();
} catch (Exception e) {
System.out.println("Inside methodB");
e.printStackTrace(System.out);
throw e;
}
}
public static void methodC() throws Exception {
try {
methodA();
} catch (Exception e) {
System.out.println("Inside methodC");
e.printStackTrace(System.out);
throw (Exception) e.fillInStackTrace();
}
}
}
Слайд 21

Перебрасывание исключений public class RethrowingDemo { public static void main(String[] args)

Перебрасывание исключений

public class RethrowingDemo {
public static void main(String[] args) {
try

{
methodB();
} catch (Exception e) {
System.out.println("Caught in main");
e.printStackTrace(System.out);
}
System.out.println();
try {
methodC();
} catch (Exception e) {
System.out.println("Caught in main");
e.printStackTrace(System.out);
}
}
}
Слайд 22

Перебрасывание исключений Originating the exception in methodA() Inside methodB java.lang.Exception: Thrown

Перебрасывание исключений
Originating the exception in methodA()
Inside methodB
java.lang.Exception: Thrown from methodA()
at classes.RethrowingDemo.methodA(RethrowingDemo.java:7)
at

classes.RethrowingDemo.methodB(RethrowingDemo.java:12)
at classes.RethrowingDemo.main(RethrowingDemo.java:32)
Caught in main
java.lang.Exception: Thrown from methodA()
at classes.RethrowingDemo.methodA(RethrowingDemo.java:7)
at classes.RethrowingDemo.methodB(RethrowingDemo.java:12)
at classes.RethrowingDemo.main(RethrowingDemo.java:32)
Originating the exception in methodA()
Inside methodC
java.lang.Exception: Thrown from methodA()
at classes.RethrowingDemo.methodA(RethrowingDemo.java:7)
at classes.RethrowingDemo.methodC(RethrowingDemo.java:22)
at classes.RethrowingDemo.main(RethrowingDemo.java:38)
Caught in main
java.lang.Exception: Thrown from methodA()
at classes.RethrowingDemo.methodC(RethrowingDemo.java:26)
at classes.RethrowingDemo.main(RethrowingDemo.java:38)
Слайд 23

Сцепленные исключения


Сцепленные исключения

Слайд 24


Слайд 25

Не сцепленные исключения public class NoChainedExceptionDemo { public static void methodA()

Не сцепленные исключения

public class NoChainedExceptionDemo {
public static void methodA() throws

Exception {
System.out.println("Originating the exception in methodA()");
throw new Exception("Thrown from methodA()");
}
public static void methodB() throws Exception {
try {
methodA();
} catch (Exception e) {
System.out.println("Throwing new exception in methodB()");
throw new Exception("Thrown from methodB()");
}
}
public static void methodC() {
try {
methodB();
} catch (Exception e) {
System.out.println("Throwing new exception in methodC()");
throw new RuntimeException("Thrown from methodC()");
}
}
public static void main(String[] args) {
methodC();
}
}
Слайд 26

Не сцепленные исключения Originating the exception in methodA() Throwing new exception

Не сцепленные исключения
Originating the exception in methodA()
Throwing new exception in methodB()
Throwing

new exception in methodC()
Exception in thread "main" java.lang.RuntimeException: Thrown from methodC()
at classes.NoChainedExceptionDemo.methodC(NoChainedExceptionDemo.java:27)
at classes.NoChainedExceptionDemo.main(NoChainedExceptionDemo.java:32)
Слайд 27

final final final Класс Throwable public class Throwable implements Serializable {

final

final

final

Класс Throwable

public class Throwable implements Serializable {
private Throwable cause =

this;
public Throwable getCause() {
return (cause==this ? null : cause);
}
public Throwable(String message, Throwable cause) {
fillInStackTrace();
detailMessage = message;
this.cause = cause;
}
public synchronized Throwable initCause(Throwable cause) {
if (this.cause != this)
throw new IllegalStateException("Can't overwrite cause");
if (cause == this)
throw new IllegalArgumentException("Self-causation not permitted");
this.cause = cause;
return this;
}
}

C

Throwable cause

Throwable cause

Throwable cause

this.cause = cause

this.cause = cause

Слайд 28

final final final Класс Throwable public class Throwable implements Serializable {

final

final

final

Класс Throwable

public class Throwable implements Serializable {
public void printStackTrace(PrintStream s)

{
synchronized (s) {
s.println(this);
StackTraceElement[] trace = getOurStackTrace();
for (int i=0; i < trace.length; i++)
s.println("\tat " + trace[i]);
Throwable ourCause = getCause();
if (ourCause != null)
ourCause.printStackTraceAsCause(s, trace);
}
}
private void printStackTraceAsCause(PrintStream s, StackTraceElement[] causedTrace) {
StackTraceElement[] trace = getOurStackTrace();
int m = trace.length-1, n = causedTrace.length-1;
while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {
m--; n--;
}
int framesInCommon = trace.length - 1 - m;
s.println("Caused by: " + this);
for (int i=0; i <= m; i++)
s.println("\tat " + trace[i]);
if (framesInCommon != 0)
s.println("\t... " + framesInCommon + " more");
Throwable ourCause = getCause();
if (ourCause != null)
ourCause.printStackTraceAsCause(s, trace);
}
public Throwable getCause() {
return (cause==this ? null : cause);
}
}

printStackTraceAsCause

printStackTraceAsCause

Слайд 29

Сцепленные исключения public class ChainedExceptionDemo { public static void methodA() throws

Сцепленные исключения

public class ChainedExceptionDemo {
public static void methodA() throws Exception

{
System.out.println("Originating the exception in methodA()");
throw new Exception("Thrown from methodA()");
}
public static void methodB() throws Exception {
try {
methodA();
} catch (Exception e) {
System.out.println("Throwing new exception in methodB()");
throw new Exception("Thrown from methodB()", e);
}
}
public static void methodC() {
try {
methodB();
} catch (Exception e) {
System.out.println("Throwing new exception in methodC()");
throw new RuntimeException("Thrown from methodC()", e);
}
}
public static void main(String[] args) {
methodC();
}
}
Слайд 30

Сцепленные исключения Originating the exception in methodA() Throwing new exception in

Сцепленные исключения
Originating the exception in methodA()
Throwing new exception in methodB()
Throwing new

exception in methodC()
Exception in thread "main" java.lang.RuntimeException: Thrown from methodC()
at classes.ChainedExceptionDemo.methodC(ChainedExceptionDemo.java:27)
at classes.ChainedExceptionDemo.main(ChainedExceptionDemo.java:32)
Caused by: java.lang.Exception: Thrown from methodB()
at classes.ChainedExceptionDemo.methodB(ChainedExceptionDemo.java:18)
at classes.ChainedExceptionDemo.methodC(ChainedExceptionDemo.java:24)
... 1 more
Caused by: java.lang.Exception: Thrown from methodA()
at classes.ChainedExceptionDemo.methodA(ChainedExceptionDemo.java:10)
at classes.ChainedExceptionDemo.methodB(ChainedExceptionDemo.java:15)
... 2 more