プログラミング
 Computer >> コンピューター >  >> プログラミング >> プログラミング

JSPで例外を処理する方法は?


タグはスロー可能をキャッチします それはその本体で発生し、オプションでそれを公開します。これは、エラー処理と問題へのより適切な対処に使用されます。

属性

タグには次の属性があります-

属性 説明 必須 デフォルト いいえ なし
var 本体の要素によってスローされた場合にjava.lang.Throwableを保持する変数の名前。

<%@ taglib uri = "https://java.sun.com/jsp/jstl/core" prefix = "c" %>
<html>
   <head>
      <title><c:catch> Tag Example</title>
   </head>
   <body>
      <c:catch var ="catchException">
         <% int x = 5/0;%>
      </c:catch>
      <c:if test = "${catchException != null}">
         <p>The exception is : ${catchException} <br />
         There is an exception: ${catchException.message}</p>
      </c:if>
   </body>
</html>
>

上記のコードは次の結果を生成します-

The exception is : java.lang.ArithmaticException: / by zero
There is an exception: / by zero

  1. Pythonで例外を処理する方法は?

    Pythonで例外を処理する最も簡単な方法は、「try-except」ブロックを使用することです。 例 try: fob = open("test.txt", "r") fob.write("This is my test file for exception handling!!") except IOError: print "Error: can\'t find the file or read data" else: print "Write operation is performed

  2. ループ内でPython例外を処理する方法は?

    次のように書き直すことで、コードの例外を作成できます a=[] foo = 'redbullz' try: for i in foo: a.append(i) print a[8] except Exception as e: print e 次の出力が得られます list index out of range Process finished with exit code 0