안드로이드 개발

IOException 해결 방법

피커 2023. 5. 10. 22:33
728x90
반응형

안드로이드 개발시 흔하게 보이는 에러중에 하나입니다.

보통 입출력 관련 에러에서 흔하게 발생합니다.

예를 들면 파일을 기록하거나 읽어올때 발생합니다.

사실, 개발하다보면 여러가지 경우에 발생하는게 바로 IOException입니다.

 

1. 에러 

  -. 아래는 에러 발생시의 로그입니다.

error: unreported exception IOException; must be caught or declared to be thrown
        input.readLine();

 

2. 에러 코드 (오류 포함)

 

3. 해결 방법

  -. 위 에러의 경우는 에러 발생 가능성이 있는 부분에 throw 처리를 안하고 있기 때문입니다.

아래처럼 throw 할 수 있는 exception을 추가해주면 문제는 자연스럽게 해결이 됩니다.

 

4. 백서 (white paper)

Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.

Summary

Public constructors
Constructs an IOException with null as its error detail message.
IOException(message: String!)
Constructs an IOException with the specified detail message.
IOException(message: String!, cause: Throwable!)
Constructs an IOException with the specified detail message and cause.
Constructs an IOException with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).

Public constructors

IOException

Added in API level 1
 
IOException()

Constructs an IOException with null as its error detail message.

IOException

Added in API level 1
 
IOException(message: String!)

Constructs an IOException with the specified detail message.

Parameters
message String!: The detail message (which is saved for later retrieval by the getMessage() method)

IOException

Added in API level 9
 
IOException(
    message: String!, 
    cause: Throwable!)

Constructs an IOException with the specified detail message and cause.

Note that the detail message associated with cause is not automatically incorporated into this exception's detail message.

Parameters
message String!: The detail message (which is saved for later retrieval by the getMessage() method)
cause Throwable!: The cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)

IOException

Added in API level 9
 
IOException(cause: Throwable!)

Constructs an IOException with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause). This constructor is useful for IO exceptions that are little more than wrappers for other throwables.

Parameters
cause Throwable!: The cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
반응형