Java Tutorial/JSP/Exception
Содержание
- 1 Catching an ArithmeticException Exception
- 2 Catching an ArithmeticException Exception
- 3 Catching an ArrayIndexOutOfBoundsException Exception
- 4 Causing a Runtime Error
- 5 Creating a Custom Exception Object
- 6 Nesting try/catch Statements
- 7 Printing a Stack Trace to the Server Console
- 8 Throwing Exceptions From Methods
- 9 Using a try/catch Block
Catching an ArithmeticException Exception
<source lang="java">
<HTML>
<HEAD> <TITLE>Catching an ArithmeticException Exception</TITLE> </HEAD> <BODY>
Catching an ArithmeticException Exception
<% try { int array[] = new int[100]; array[100] = 100; } catch (ArrayIndexOutOfBoundsException e) { out.println("Array index out of bounds."); } catch(ArithmeticException e) { out.println("Arithmetic exception: " + e); } catch(Exception e) { out.println("An error occurred: " + e); }
%> </BODY>
</HTML></source>
Catching an ArrayIndexOutOfBoundsException Exception
<source lang="java">
<HTML>
<HEAD> <TITLE>Catching an ArrayIndexOutOfBoundsException Exception</TITLE> </HEAD> <BODY>
Catching an ArrayIndexOutOfBoundsException Exception
<% try { int array[] = new int[100]; array[100] = 100; } catch (ArrayIndexOutOfBoundsException e) { out.println("Array index out of bounds."); } %> </BODY>
</HTML></source>
Causing a Runtime Error
<source lang="java">
<HTML>
<HEAD> <TITLE>Causing a Runtime Error</TITLE> </HEAD> <BODY>
Causing a Runtime Error
<% int int1 = 1; int1 = int1 / 0; out.println("The answer is " + int1); %> </BODY>
</HTML></source>
Creating a Custom Exception Object
<source lang="java">
<HTML>
<HEAD> <TITLE>Creating a Custom Exception Object</TITLE> </HEAD> <BODY>
Creating a Custom Exception Object
<%! class MyException extends Exception { int value; public String toString() { return "MyException " + value; } MyException(int v) { value = v; } } void doWork(int value) throws MyException { if(value == 0){ throw new MyException(value); } } %> <% try { doWork(3); doWork(2); doWork(1); doWork(0); } catch (MyException e) { out.println("Exception: " + e); } %> </BODY>
</HTML></source>
Nesting try/catch Statements
<source lang="java">
<HTML>
<HEAD> <TITLE>Nesting try/catch Statements</TITLE> </HEAD> <BODY>
Nesting try/catch Statements
<% try { try { int c[] = {0, 1, 2, 3}; c[4] = 4; } catch(ArrayIndexOutOfBoundsException e) { out.println("Array index out of bounds: " + e); } } catch(ArithmeticException e) { out.println("Divide by zero: " + e); } %> </BODY>
</HTML></source>
Output error message to console
<source lang="java">
<HTML>
<HEAD> <TITLE>Building a simple error handling page.</TITLE> </HEAD> <BODY> <% try{ int value = 1; value = value / 0; } catch (Exception e){ System.out.println(e.getMessage()); } %> </BODY>
</HTML></source>
Printing a Stack Trace to the Server Console
<source lang="java">
<HTML>
<HEAD> <TITLE>Printing a Stack Trace to the Server Console</TITLE> </HEAD> <BODY>
Printing a Stack Trace to the Server Console
<% try{ int value = 1; value = value / 0; } catch (Exception e){ e.printStackTrace(); } %> </BODY>
</HTML></source>
Throwing an Exception
<source lang="java">
<html>
<head> <title>Throwing an Exception</title> </head> <body> <title>Throwing an Exception</title> <% try { throw new ArithmeticException("Math Exception!"); } catch(ArithmeticException e) { out.println("Exception message: " + e); } %> </body>
</html></source>
Throwing Exceptions From Methods
<source lang="java">
<HTML>
<HEAD> <TITLE>Throwing Exceptions From Methods</TITLE> </HEAD> <BODY>
Throwing Exceptions From Methods
<%! void doWork() throws ArrayIndexOutOfBoundsException { int array[] = new int[100]; array[100] = 100; } %> <% try { doWork(); } catch (ArrayIndexOutOfBoundsException e) { out.println("Array out of bounds exception"); } %> </BODY>
</HTML></source>
Using a try/catch Block
<source lang="java">
<HTML>
<HEAD> <TITLE>Using a try/catch Block</TITLE> </HEAD> <BODY>
Using a try/catch Block
<% try{ int int1 = 1; int1 = int1 / 0; out.println("The answer is " + int1); } catch (Exception e){ out.println("An exception occurred: " + e.getMessage()); } %> </BODY>
</HTML></source>