[Java] (Learning Memo) Java Level 2 Measures: Q4 Key Points (Exception Handling)

1 minute read

Java Self-learning notes

Question 4 Summary of points

Be familiar with exception handling

When designing a program, It is necessary to be prepared in consideration of the possibility that an unexpected error will occur at runtime: writing_hand:

① try (processing that may cause an exception) ② catch block (catch the exception you want to handle) ③ finally block (processing to be performed regardless of whether an exception occurs)

  • If you have either catch or finally, you can omit one.

  • Try-catch block cannot be described in finally block.
  • It is not possible to describe multiple finally blocks.

  • When you want to throw exception handling to the calling class without handling it yourself If you describe thows exception class name in the signature of the method, it’s OK (convenient)
try{
Statement executed if there is no error
}catch (exception class variable name){
Exception processing (statements executed at the time of exception)
}

What are the exception classes ■Error type exception: No need to catch with try-catch ■ Exception type exception: If you do not catch it with try-catch, a compilation error will occur. ■ RuntimeException type exception: OK with try-catch or not!

try{
Statement executed if there is no error
}catch (exception class variable name){
Exception handling (statements executed at the time of exception)
}finally{
Processing that is always executed regardless of whether there is an exception
}
  • ——– TBE ————-