使用try-catch java

zee*_*h91 5 java try-catch

所以,深入了解java中try-catch语句的基础知识.我仍然对语法中的一些差异感到困惑.这是我试图分析和理解的代码.另外,使用try then catch(Exception e)与说只是抛出...或者抛出新的...之间的区别...根据我的理解,try catch基本上是一种通过输出消息或传递来处理错误的方法另一种方法.但是,我认为我坚持它的语法方面.任何建设性的评论或简单的例子都是受欢迎的人.关于这个概念的一些澄清以及我的书中的示例代码的内容将会受到赞赏.

/* Invalid radius class that contains error code */
public class InvalidRadiusException extends Exception {
     private double radius;

/** Construct an exception */
public InvalidRadiusException(double radius) {
       super("Invalid radius " + radius);
       this.radius = radius;
}

/** Return the radius */
public double getRadius() {
    return radius;
 }
}


public class CircleWithException {
/** The radius of the circle */

private double radius;

/** The number of the objects created */
private static int numberOfObjects = 0;

/** Construct a circle with radius 1 */
public CircleWithException() throws InvalidRadiusException {
       this(1.0);
  }

/** Construct a circle with a specified radius */
public CircleWithException(double newRadius) throws InvalidRadiusException {
          setRadius(newRadius);
          numberOfObjects++;
}

/** Return radius */
public double getRadius() {
     return radius;
}

/** Set a new radius */
public void setRadius(double newRadius)
    throws InvalidRadiusException {
if (newRadius >= 0)
  radius =  newRadius;
else
  throw new InvalidRadiusException(newRadius);
}

/** Return numberOfObjects */
public static int getNumberOfObjects() {
      return numberOfObjects;
}

/** Return the area of this circle */
public double findArea() {
    return radius * radius * 3.14159;
 }
}
Run Code Online (Sandbox Code Playgroud)

sin*_*inθ 8

说明

Java文档:

[ try block]包含一个或多个可能引发异常的合法代码行.(catch和finally块将在接下来的两个小节中解释.)

异常是一种特殊的对象.编写时new Exception(),您正在创建一个新的异常对象.在编写时,throw new Exception()您正在创建一个新错误,然后将其抛出到最近的try-catch块,中止其余的代码.

当你抛出异常时,它会嵌套在(里面)的try-catch块捕获.也就是说,假设注册了该异常的正确catch块.如果代码没有包装在try-catch块中,则程序会在抛出错误时自动关闭.在任何可能引发错误的代码或方法周围使用try-catch,尤其是因为用户输入(在合理范围内).

必须捕获一些例外,其他例外是​​可选的.(选中与未选中).

当您添加throws到方法签名时,您将向其他方法宣布,如果它们调用该方法,则它有可能抛出已检查的异常(未选中此方法).请注意它throws不是throw.它没有采取行动,它描述了它有时会采取行动.

当您不想捕获该方法中的错误但希望允许调用方法的方法自己捕获错误时,可以使用此功能.

异常是一种使程序对意外或无效情况一致响应的方法,在需要用户输入时尤其有用,尽管它在其他情况下也很有用,例如文件输入/输出.

例子

public CircleWithException() throws InvalidRadiusException {
       this(1.0);
}
Run Code Online (Sandbox Code Playgroud)

在这里,CircleWithException()有可能抛出一个InvalidRadiusException(可能,这个(1.0)有时会抛出一个InvalidRadiusException.)

调用此方法的代码应该具有:

try {
    new CircleWithException(); // This calls the method above
} catch (InvalidRadiusException e) { // The object "e" is the exception object that was thrown.
    // this is where you handle it if an error occurs
}
Run Code Online (Sandbox Code Playgroud)

正如我之前所说,Exception只是一种特定类型的对象Exception

/* Invalid radius class that contains error code */
public class InvalidRadiusException extends Exception {
     private double radius;

/** Construct an exception */
public InvalidRadiusException(double radius) {
       super("Invalid radius " + radius);
       this.radius = radius;
}

/** Return the radius */
public double getRadius() {
    return radius;
 }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码定义了一种特定于程序/应用程序的新类型的Exception.Java标准库中有许多预定义的异常,但通常需要创建自己的异常.

要抛出此异常,首先要创建一个InvalidRadiusException对象,然后抛出它:

throw new InvalidRadiusException(1.0);
Run Code Online (Sandbox Code Playgroud)