# data[].attributes.headline
Write your summary of Chapter 5 - Exceptions and Assertions here!

# data[].attributes.subject
Write your summary of Chapter 5 - Exceptions and Assertions here!

# data[].attributes.body
Dear Section 1A, kindly post your summary of Chapter 5 - Exceptions and Assertions here! Don't forget to write your full name and student ID as well ya. Good luck all!

# data[].attributes.body
Chapter 5 is about learning this exception ( a type of invalid or abnormal occurance during a normal course of a program) in java. We learning 3 catergories of exception which is checked exception, uncheck exception and errors. Beside that, we learn different methods to catch exception such as try/catch,throw, the finally block and assertions

# data[].attributes.headline
Nur Qistina Binti Noor Shaifull (DC98943)

# data[].attributes.subject
Nur Qistina Binti Noor Shaifull (DC98943)

# data[].attributes.body
Chapter 5 Exception and Assertion

3 questions will be in finals MCQ

Introduction

In Java, instead of getting error message, you can throw an Exception object to the caller and caller will handle the exception.

Exception occurs when normal flow of program gets disrupted and programs get terminated abnormally, which is not recommended.

Can be caused by user error, programmer error, or physical resource that failed.

three categories of exceptions:-

Checked exceptions:

Occurs during compile time (compile time exceptions)

Cannot be ignored at compilation time, must be handled by programmer.

Example: FileNotFoundException

Unchecked exceptions

Occurs during time of execution (Runtime exceptions)

programming bugs such as logic errors or improper use of API

ignored during compilation

Example: ArrayIndexOutOfBpund Exception

Errors

Not exception, but problem beyond control

Usually ignored since it can't be handled

Example: StackOverFlowError

Exception

An error that occur during normal course of program execution.

When occurs (thrown), normal sequence of flow is terminated, exception-handling routine executed (caught)

Try and catch

Catches exception inside code using try block, and will be handled by catch block.

Catch: give message to user when exception occurs

Throw

Used to throw an exception explicitly.

Finally

Used when certain actions need to be taken regardless of whether exception is thrown or not.

Can be used for cleanup code after the try-catch statement.

Assertion

Used to detect logical errors

Expression must be true otherwise AssertionError is thrown.

3 type of assertion:

1. Post-condition assertion

2. Pre-condition assertion

3. Control flow invariant

# data[].attributes.headline
Nur Alia Adziani binti Abdul Aziz (Dc99016)

# data[].attributes.subject
Nur Alia Adziani binti Abdul Aziz (Dc99016)

# data[].attributes.body
Chapter 5 – Exceptions & Assertions

Exceptions are errors that happen while a program is running.

When an exception happens, the normal flow of the program stops.

Example causes:

User enters wrong input

File cannot be found

Internet connection lost

Program runs out of memory

There Are 3 categories

-Checked Exception

Happen at compile time.

Java forces you to handle them

Unhecked Exceptions

Happen at runtime

caused by programming mistakes

Errors

very serious issues

cannot be handle by the program

Handle Exceptions

Try

-code that might cause an error

Catch

-Handle that error if it happens

Finally

-always run, wheter there is an error or not

-Good for cleanup

Assertions

-Assertion are used to detect logic errors during development

Types:

Pre-condition (before method runs)

Post-condition (after operation)

Control-flow invariant (should always be true)

# data[].attributes.headline
Nur Aina Aliah Binti Sukar @ Yahya (DC98942)

# data[].attributes.subject
Nur Aina Aliah Binti Sukar @ Yahya (DC98942)

# data[].attributes.body
Exceptions and Assertions Summary

This chapter explains how to manages errors using exceptions and assertions. Exceptions interrupt normal program flow, so Java provides try-catch blocks and the finally block to handle them. Exceptions are divided into checked (compile time), unchecked (runtime), and errors (serious system failure). The throw keyword can be used to manually create exceptions. Assertions are used to verify logical conditions during development. If an assertion fail, the program throws an AssertionError. There are 3 types of assertion including Post-condition assertion, Pre-condition assertion, and Control flow invariant.

note: will come out in final Section A (MCQ, only 3 question)

# data[].attributes.body
Exceptions are problems that happen while a program is running, and Java handles them using try, catch, and blocks. Types of exception checked exception, unchecked exception, and error. You can also throw your own exception using throw. Java also has assertion, which check if something in your program should logically be true; if it’s false, the program stops with an error. This helps you find mistakes during development.

# data[].attributes.body
Exceptions

Exceptions are errors that happen while a program is running.
When an exception occurs, the normal flow of the program stops.

Example causes

User enters wrong input

File cannot be found

Internet connection lost

Program runs out of memory

3 Categories of Exceptions

Checked Exception

Happen at compile time

Java forces you to handle them

Unchecked Exception

Happen at runtime

Caused by programming mistakes

Errors

Very serious issues

Cannot be handled by the program

Handling Exceptions

try

Code that might cause an error

catch

Handles the error if it happens

finally

Always runs, whether an error occurs or not

Good for cleanup

Assertions

Assertions are used to detect logic errors during development.

Types

Pre-condition (before method runs)

Post-condition (after operation)

Control-flow invariant (should always be true)

# data[].attributes.body
Exceptions are unexpected events that occur while a program is running. When an exception happens, the normal flow of the program is interrupted.

Common causes include:

The user enters invalid input

A file that the program needs cannot be found

The internet connection is lost

The program runs out of memory

Java classifies exceptions into three main groups:

1. Checked Exceptions

Detected during the compilation stage

Java requires the programmer to handle them using a try–catch block or by declaring them with throws

Example: FileNotFoundException

2. Unchecked Exceptions (Runtime Exceptions)

Occur while the program is being executed

Usually caused by programming mistakes such as logic errors or incorrect use of an API

The compiler does not force programmers to handle them

Example: ArrayIndexOutOfBoundsException

3. Errors

More serious issues that are generally outside the program’s control

These problems cannot usually be handled by the application

# data[].attributes.headline
Levin Matthew A/L Chandrasegaran (DC98967)

# data[].attributes.subject
Levin Matthew A/L Chandrasegaran (DC98967)

# data[].attributes.body
Chapter 5: Exceptions and Assertion

Definition of Exception -> Represents an error condition that occurs during a program execution

Instead of an error message, the compiler throwss a exception object like IllegalArgumentException to the caller and let it handle it.

Exception occurs when the program contains invalid data, a file can't be found and in network connection issues. The exception can be caused by the programmer errror.

Three categories of exception;

Checked exception -> occurs during compile time

unchecked exception -> occurs during execution time

errors -> handled by programmer and its usually not an exception

Try and Catch blocks

Its a method to catch an exception of these two blocks. Every try block comes with a finally block.

Catch: Gives the message to the user when error occurs.

Throw keyword is used to throw an exception explicitly.

Finally Block: If no error-> DONE

If got error -> "Not an integer"

Assertion -> Used to detect logical errors in program(true or false)

The syntax used is "assert" <boolean expression>

3 Types of assertion: Pre-condition, post condition and control flow invariant.

# data[].attributes.body
This chapter explain about exception and assertion. Exception is is an error that happens while the program is running, such as dividing by zero, accessing an invalid array index, or opening a file that does not exist. When an exception occurs, it interrupts the normal flow of the program unless it is handled using a try–catch or throw-else block. Exceptions are used to deal with problems that can realistically happen during program execution. On the other hand, an assertion is a tool used by developers to check logical error that are supposed to be true during the development or debugging stage. There are 3 types of assertion which is Post-condition assertion, Pre-condition assertion and control flow invariant.

# data[].attributes.body
Chapter 5 covers how Java handles abnormal or invalid events using exceptions and assertions. Exceptions are grouped into 3 categories: checked, unchecked, and errors. Java manages exceptions using try-catch blocks, manually throwing exceptions with throw, executing cleanup code with finally, and validating logic during development using assertions. When an assertion fails, an AssertionError is thrown.

# data[].attributes.headline
Muhammad Akmal Hariz bin Mohd Fadzli(dc98999)

# data[].attributes.subject
Muhammad Akmal Hariz bin Mohd Fadzli(dc98999)

# data[].attributes.body
Chapter 5: Exception and Assertion

In the final exam, 3 MCQ questions will be asked from this chapter.

Introduction

In Java, instead of showing an error message directly, a program can throw an Exception object. This exception is sent to another part of the program, where it is handled properly.

An exception happens when the normal execution of a program is interrupted. If exceptions are not handled, the program may stop running suddenly, which is not a good practice.

Exceptions can occur due to:

Mistakes made by the user

Errors made by the programmer

Failure of system resources (like memory or files)

Types of Exceptions

There are three main categories:

1. Checked Exceptions

Occur during compilation

The compiler forces the programmer to handle them

Cannot be ignored

Example: FileNotFoundException

2. Unchecked Exceptions

Occur at runtime (during execution)

Usually caused by programming mistakes such as wrong logic or incorrect API usage

Not checked at compile time

Example: ArrayIndexOutOfBoundsException

3. Errors

These are not exceptions

Caused by serious problems that are outside the control of the program

Generally cannot be handled

Example: StackOverflowError

Exception

An exception is a problem that occurs while the program is running normally.
When an exception is thrown, the normal flow of the program stops, and the program looks for an exception-handling block to manage it.

Try and Catch

The try block contains code that may cause an exception

The catch block handles the exception

The catch block usually shows an error message or performs corrective actions

Throw

The throw keyword is used to manually generate an exception

It allows the programmer to pass the exception to another part of the program

Finally

The finally block executes whether an exception occurs or not

Commonly used for cleanup tasks, such as closing files or releasing resources

Assertion

Assertions are used to find logical mistakes in a program.
An assertion checks whether a condition is true. If the condition is false, an AssertionError is thrown.

Types of Assertions

1. Pre-condition assertion – checks conditions before executing a statement

2. Post-condition assertion – checks conditions after execution

3. Control flow invariant – ensures conditions remain true during program execution