Errors in code Handling exceptions in Java, Schemes and Mind Maps of Software Development

When the exception-handling code is executed, the error is caught. • Examples: ... Exception in thread main java.util.InputMismatchException at java.util.

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 02/28/2023

edmond
edmond 🇺🇸

3.8

(10)

251 documents

1 / 55

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming KM2 - Software Development Technician
Errors in code
Handling exceptions in Java
Programming
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

Partial preview of the text

Download Errors in code Handling exceptions in Java and more Schemes and Mind Maps Software Development in PDF only on Docsity!

Errors in code

Handling exceptions in Java

Programming

  • What is a good program? A program that is reliable.
  • Not just giving correct answer on correct input
  • Should protect against possible errors (invalid password, not a picture file, etc) Throwing Exceptions

How do we do this?

  • Option 1 – return a special value to determine if the method succeeded or failed - Ex. Make withdraw return true or false
  • What’s wrong with this?
    • Calling method may not check answer
    • Calling method may not know what to do

Catching Exceptions

  • Exception – an error condition that can occur during the normal course of a program execution
  • In Java, exceptions are objects themselves
  • Exception handling is another form of control structure (like ifs and switch statements) - When an error is encountered, the normal flow of the

program is stopped and the exception is handled

Exception Program Flow

  • What happens when exceptions occur?
    • An Exception object is thrown
  • What happens when an Exception is thrown?
    • normal execution stops and exception handling begins
  • What does the Exception object know?
    • the name of the problem
    • the location where it occurred
    • and more…

Why have exception handling?

  • consistency (everyone else does it)
    • Java API classes use exceptions.
    • Other programming languages do too!
  • flexibility
    • Programmer can decide how to fix problems.
  • simplicity
    • Easy to pinpoint problems.

public double divide( int data1, int data2 )

return data1 / data2;

What exceptions can occur?

Arithmetic Exception:

divide by zero

How to handle Exceptions?  Do nothing

  • program crashes if the exception occurs!  Propagate (throws) it
  • tell the method’s caller about it and let them decide what to do  Resolve (try-catch) it in our method
  • fix it or tell the user about it and let them decide what to do
  • Says in English:
    • System has caught an error described as a

InputMismatchException

  • Thrown because a String cannot be converted to an integer
  • When system handles, we often get a program crash
  • Instead of the system, we can handle to improve robustness

Throwing Exceptions

  • If there is an error in the value of a parameter, we can throw exceptions to make the user accountable
  1. Decide what type of exception to throw
  2. Test for condition, and throw the exception if condition is violated

Problem public class BankAccount { public void withdraw(double amount) { if (amount > balance) { ????????? } balance = balance - amount; }

... }

Solution #

public class BankAccount

public void withdraw(double amount)

if (amount > balance)

IllegalArgumentException exception

= new IllegalArgumentException("Amount

exceeds balance");

throw exception;

balance = balance - amount;

Checked/Unchecked Exceptions

  • Checked Exception – checked at compile time - Complier ensures that you are handling a possible problem - Due to external circumstances that the programmer cannot prevent - Majority occur when dealing with input and output - For example, IOException
  • Unchecked Exception – Runtime Exceptions
    • Extend the class RuntimeException or Error
    • They are the programmer's fault
    • Examples of runtime exceptions:
      • NumberFormatException
      • IllegalArgumentException
      • NullPointerException
    • Optional to deal with these
  • Example of error: OutOfMemoryError
    • Can’t do anything about these catastrophic problems, so don’t deal with it