Using try-catch java

So I got into the basics of try-catch statements in Java, and I'm still a bit confused about some of the differences in syntax.

Here is the code I'm trying to analyze and understand. What is the difference between using tryand catch(Exception e)comparing to pronouncing simply throwsor throw new?

As far as I understand, tryand catch- this is basically a way to handle the error by displaying a message or passing it to another method. However, I think I'm stuck on the syntactic aspect of this.

Any constructive comments or simple examples explaining this concept, as well as what happens to the sample code from my book, will 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;
 }
}
+5
4

Java:

[ try] , . ( , , .)

- . new Exception(), . throw new Exception(), , try-catch, .

, try-catch, (). , , catch . try-catch, , . try-catch , , - ( ).

, - . ( ).

throws , , , , checked ( ). , throws throw. , , .

, , , , .

- , , , / .

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

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
}

, - , 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 , .

, InvalidRadiusException, :

throw new InvalidRadiusException(1.0);
+8

, ( ) .

setRadius . , ( setRadius) , try-catch:

public CircleWithException(double newRadius) throws InvalidRadiusException {
    try {
        setRadius(newRadius);
        numberOfObjects++;
    } catch (InvalidRadiusException e) {
        setRadius(0); // for example
    }
}

catch , , . 0, .

, , , . , , ( ) .

+3

" throws" - , . java , .

" throw new" - java, ...

  • " throw" - ,
  • " " - ,

try " , , , , catch, .

, try-with-resources, try (, ), AutoCloseable, .

finally " try, , try, , .

+2

1) . Error RuntimeException . Java - .

2) . Throwable , , (1). Java try/catch. , ( throws) try, .

throws: , , try/catch.

public void doSomething() throws MyException { ... }

- , MyException.

try/ catch/ finally: This is a way to handle exceptions that can be thrown using some kind of code. The trycode you are trying to run is in the block , the error handling code goes into the block catch. An optional block finallyis what will be executed regardless of whether an exception is thrown. In fact, the finally block will be called even if you execute returninside yours tryor yours catch. for instance

try {
    doSomething();
} catch (MyException exception) {
    exception.printStackTrace();
}

or usage example finally:

MyResource myResource = getMyResource();
try {
    myResource.open();
    doStuffWithMyResource(myResource);
} catch (Exception exception) {
    exception.printStackTrace();
} finally {
    if (myResource.isOpen()) {
        myResource.close();
    }
}
+2
source

Source: https://habr.com/ru/post/1533398/


All Articles