[java] throw an exception

Exception handling is the basis of java, but since it was misrecognized, it is left as a memorandum.

Problems that occurred

I wanted to throw an exception to the method caller, so when I tried to throw the caught exception, I got a compile error for some reason.

Implementation

For example, when throwing a timeout error, it is as follows.

    public void foo(){
        try {
            fetch();
        } catch (SocketTimeoutException e) {
        }
    }

    public Integer fetch(){
        try {
 //Http通信
        } catch (SocketTimeoutException e) {
 throw e; // compile error here
        }finally {
        }
        return number;
    }

Misunderstanding

Besides throw There is a thorws clause, but I thought it had the same function as the catch statement. But in fact the method with it meant ** a method that could throw an exception **.

So I added a throws clause to the method throwing the exception and the compile error disappeared and I could catch it with foo ().

Impressions

I didn't think I would stumble on the foundation of the foundation, but I'm glad I knew it now ...

Recommended Posts

[java] throw an exception
Java exception handling?
[Java] Exception instance
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
Questions in java exception handling throw and try-catch
Java basic learning content 7 (exception)
About an instance of java
[Java] Practice of exception handling [Exception]
[Java] About try-catch exception handling
Java exception handling usage rules
Java creates an Excel document
Exception handling techniques in Java
Division by zero Java double does not raise an exception
Create an immutable class with JAVA
I sent an email in Java
Java learning memo (creating an array)
exception
[Java] Declare and initialize an array
Try an If expression in Java
Java
Unexpected exception when using Java DateTimeFormatter
[Java] What is Concurrent Modification Exception?
I made an annotation in Java.
Formatting an enum using formatter-maven-plugin (Java)
[Introduction to Java] About exception handling (try-catch-finally, checked exception, unchecked exception, throws, throw)
Java
Step-by-step understanding of Java exception handling
[For Java beginners] About exception handling
Run an application made with Java8 with Java6
[Java] Exception types and basic processing
Run an external process in Java
[Java] Create an executable module with Gradle
Map without using an array in java
[Java] Send an email using Amazon SES
Compare the elements of an array (Java)
Java (exception handling, threading, collection, file IO)
Java Exception StackTrace is determined when new
try-catch-finally exception handling How to use java