Java/JSP/Try Catch
Содержание
Multiple Catch
<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."); } catch(ArithmeticException e) { out.println("Arithmetic exception: " + e); } catch(Exception e) { out.println("An error occurred: " + 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>
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>
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 i = 1; i = i / 0; out.println("The answer is " + i); } catch (Exception e){ out.println("An exception occurred: " + e.getMessage()); } %> </BODY>
</HTML>
</source>