使用try-catch java [英] Using try-catch java

查看:111
本文介绍了使用try-catch java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So just got into the basics of try-catch statements in java. Im still a bit confused on some differences within the syntax. Here is the code im trying to analyze and understand. Also, whats the difference between using try then catch(Exception e) as compared to saying just throws...or throw new... From my understanding, a try catch is basically a way to handle the error by outputting a message or passing to another method. However, i think im stuck on the syntax aspect of it. Any constructive comments or simple examples are welcome guys. Some clarification on this concept as well as whats going on with the sample code from my book would be appreciated.

/* 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;
 }
}


推荐答案

说明



Java文档


[尝试块]包含一个或多个合法的
可以抛出异常的代码行。 (最后两个小节将解释捕获和最后的
块。)

[The try block] contains one or more legal lines of code that could throw an exception. (The catch and finally blocks are explained in the next two subsections.)

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

An exception is a special kind of object. When you write new Exception(), you are creating a new exception object. When you write throw new Exception() you are creating a new error, and then throwing it to the nearest try-catch block, aborting the rest of your code.

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

When you throw an exception, it gets caught by the try-catch block that it's nested in (inside of). That is, assuming the proper catch block for that exception is registered. If the code is not wrapped in a try-catch block, the program with automatically shut down as soon as an error is thrown. Use a try-catch around any code or method that can throw an error, especially because of user input (within reason).

必须捕获一些例外,其他的是可选择捕获。 (已检查与未选中)。

Some exceptions have to be caught, others are optional to catch. (checked vs. unchecked).

当您将抛出添加到方法签名时,您会向其他方法宣布,如果他们调用该方法,则有可能抛出已检查异常(未经检查就没有必要)。注意它是如何抛出而不是抛出。它没有做一个动作,它描述它有时会做一个动作。

When you add throws to a method signature, you are announcing to other methods that if they call that method, it has the potential to throw a checked exception (it is not necessary for unchecked). Notice how it's throws not throw. It's not doing an action, it's describing that it sometimes does an action.

当你不想在该方法中捕获错误但想要使用此功能时允许调用方法的方法自己捕获错误。

You use this functionality when you don't want to catch the error inside that method, but want to allow the method's that call your method to catch the error themselves.

异常是让程序对意外或无效情况做出一致响应的一种方法,在需要用户输入时特别有用,尽管它在其他情况下也很有用,例如File输入/输出。

Exceptions are a way to make your program respond coherently to unexpected or invalid situations and are especially useful when user input is required, though it's also useful in other situations such as File input/output.

public CircleWithException() throws InvalidRadiusException {
       this(1.0);
}

这里, CircleWithException()具有抛出InvalidRadiusException电位(。据推测,这(1.0)有时将引发InvalidRadiusException)

Here, the CircleWithException() has the potential to throw an InvalidRadiusException (presumably, the this(1.0) sometimes throws an InvalidRadiusException.)

代码调用此方法应具有:

The code calling this method should have:

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
}

正如我之前所说,异常只是一种特定类型的对象,扩展异常

As I said before, an Exception is just a specific type of object that extends 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;
 }
}

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

The above code defines a new type of Exception specific to your program/application. There are many predefined exceptions in the Java Standard Library, but often you need to create your own.

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

To throw this exception, you first create an InvalidRadiusException object and then throw it:

throw new InvalidRadiusException(1.0);

这篇关于使用try-catch java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆