๐Ÿ’ป

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception) ๋ณธ๋ฌธ

KITRI/JAVA

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception)

๋˜ํšจ๋‹ˆ 2020. 5. 18. 13:19

์•„๋ž˜์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ๋ณ„๊ฑฐ ์•„๋‹Œ ์ฝ”๋“œ์ด์ง€๋งŒ, ์˜ค๋ฅ˜๊ฐ€ ๋‚  ๋ถ€๋ถ„๋“ค์ด ๋งŽ๋‹ค.

์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๊ฐ€ ์ •์ˆ˜ํ˜•์ด ์•„๋‹๊ฒฝ์šฐ, ๋‚˜๋ˆ„๋Š” ์ˆ˜๊ฐ€ 0์ผ ๊ฒฝ์šฐ ๋“ฑ๋“ฑ 

 

์˜ˆ์™ธ ์ฒ˜๋ฆฌ

 

 

์˜ˆ์ œ 1) 

 

 

0์œผ๋กœ ๋‚˜๋ˆŒ๋•Œ์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์ž.

 

 

 

ArithmeticException

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/ArithmeticException.html

 

ArithmeticException (Java SE 14 & JDK 14)

All Implemented Interfaces: Serializable public class ArithmeticException extends RuntimeException Since: 1.0 See Also: Serialized Form Constructor Summary Constructors  Constructor Description ArithmeticException() Constructs an ArithmeticException with

docs.oracle.com

 

ArithmeticException ๋Œ€์‹ ์— ์ƒ์œ„ ํด๋ž˜์Šค์ธ RuntimeException, Throwable๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

NumberFormatException

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/NumberFormatException.html

 

NumberFormatException (Java SE 14 & JDK 14)

All Implemented Interfaces: Serializable public class NumberFormatException extends IllegalArgumentException Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the approp

docs.oracle.com

์˜ˆ์ œ 2)

 

 

์˜ˆ์ œ 3) ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋‹ค์Œ ์˜ˆ์ œ๋ฅผ ์‹ค์Šตํ•ด๋ณด์ž. 

 

args์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•

์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค > run > run configuration > arguments์— ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋„ฃ๊ณ  > run ํด๋ฆญ

 

๋ฐ์ดํ„ฐ๊ฐ€ 2๊ฐœ๊ฐ€ ํ•„์š”ํ•œ๋ฐ, 2๊ฐœ๋ณด๋‹ค ํฐ๊ฒฝ์šฐ๋Š” ์ƒ๊ด€์—†์ง€๋งŒ ๋ถ€์กฑํ•œ ๊ฒฝ์šฐ ERROR ๊ฐ€ ์ƒ๊ธด๋‹ค. 

์˜ˆ์ œ 4) 

 

 

 

NullPointerException

: ์ž๋ฐ”์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์ƒ๊ธฐ๋Š” ์—๋Ÿฌ

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/NullPointerException.html

 

NullPointerException (Java SE 14 & JDK 14)

All Implemented Interfaces: Serializable public class NullPointerException extends RuntimeException Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessin

docs.oracle.com

์˜ˆ์ œ 5) 

 

 

 

 

ClassCastException

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/ClassCastException.html

 

ClassCastException (Java SE 14 & JDK 14)

All Implemented Interfaces: Serializable public class ClassCastException extends RuntimeException Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a Cla

docs.oracle.com

์˜ˆ์ œ 6) 

 

 

 

 

InputMismatchException

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/InputMismatchException.html

 

InputMismatchException (Java SE 14 & JDK 14)

All Implemented Interfaces: Serializable public class InputMismatchException extends NoSuchElementException Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the

docs.oracle.com

์˜ˆ์ œ 7)

 

 

 

 

์˜ˆ์ œ 8)

๋‹ค์ค‘ catch๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. 

 

 

java.lang.Throwable

     java.lang.Exception

           java.lang.RuntimeException

 

์˜ˆ์™ธํด๋ž˜์Šค๋Š” ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„ ์œ„์˜ 3๊ฐ€์ง€๋ฅผ ์ƒ์œ„ํด๋ž˜์Šค๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ ์ƒ์œ„ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด์„œ ๋‹ค์ˆ˜์˜ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ ๋กœ์ง์ƒ ๋””ํ…Œ์ผํ•˜๊ฒŒ ๋‚˜๋ˆ ์•ผํ• ๋•Œ(์˜ˆ: ๋กœ๊ทธ์ธ ์‹œ ์•„์ด๋””์ฒ˜๋ฆฌ, ํŒจ์Šค์›Œ๋“œ ์ฒ˜๋ฆฌ) ์—๋Š” ๋”ฐ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ•œ๋‹ค. ๋””ํ…Œ์ผํ•˜๊ฒŒ ์‚ฌ์šฉ์ž ๋ฉ”์„ธ์ง€๋ฅผ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผํ•œ๋‹ค. 

 

 

 

 

 

์˜ˆ์™ธ ๋ฐœ์ƒ

: ์ปดํŒŒ์ผ ์ž…์žฅ์—์„œ๋Š” ์—๋Ÿฌ๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ, ๋‚ด ํ”„๋กœ๊ทธ๋žจ์ƒ์—์„œ๋Š” ์—๋Ÿฌ(์˜ˆ๋ฅผ๋“ค๋ฉด, ์€ํ–‰๊ณ„์ขŒ ๊ฐ™์€ ๊ฒฝ์šฐ -๋ฅผ ๋นผ๊ณ  ์ž…๋ ฅํ•˜๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ)

 

์˜ˆ์ œ 10)

 

 

์˜ˆ์™ธ ์ „๊ฐ€

 

 

 

๋ฐ˜์‘ํ˜•
Comments