Introduction to Computer Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Computer Programming

Description:

Title: Introduction to Computer Programming Subject: Looping Around Loops I: Counting Loops Author: Robert M. Siegfried Last modified by: Kristin Pepper – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 12
Provided by: Rober1411
Learn more at: https://home.adelphi.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Computer Programming


1
Introduction to Computer Programming
  • Error Handling

2
What to do with errors?
  • Return something the caller will understand
  • Public static int myMethod(int x)
  • if (x lt 0) return -1
  • else do what you want the method to do
  • Throw Exception
  • Public static int myMethod(int x)
  • if (x lt 0)
  • throw new IllegalArgumentException(x is
    negative and must be 0 or greater)
  • else do what you want the method to do

3
What is an Exception?
  • Java has objects called Exceptions that carry
    information about errors.
  • IllegalArgumentException is one type
  • When code throws an exception, it stops
    processing the method without a proper return.
  • When an exception is a Checked type, it also
    stops the program from running if noone deals
    with it.

4
Make an Exception Happen
  • import java.util.Scanner
  • public class Scan
  • public static int getNumb()
  • Scanner rdr new Scanner(System.in)
  • System.out.println("Enter a number from 1 to
    10")
  • int entry rdr.nextInt()
  • return entry

5
What happens to the calling program when an
Exception happens?
  • If the caller does not handle it
  • Crash
  • Like your Scanner programs that received letters
    instead of numbers
  • If the caller handles it
  • Does not crash
  • Does not look at return value

6
How to code Exception Handling
  • Try/Catch block
  • public static void main()
  • int x
  • try
  • x getNumb()
  • catch (Exception e)
  • System.out.println("X was never
    filled")
  • System.out.println("Every line will print
    here")

7
You Try a Try/Catch Block
  • Given this method
  • public static int times6(int x)
  • if (x lt 0)
  • throw new IllegalArgumentException(x "is
    negative and must be 0 or greater")
  • else return 6 x
  • Code a caller method that takes in 1 number and
    uses it to call times6.
  • When the method times6 returns normally, print
    the returned number.
  • When the method times6 throws an exception, print
    oops!
  • Always print The End

8
Answer to Try/Catch practice
  • public static void caller (int a)
  • try
  • int y times6(a)
  • System.out.println(y)
  • catch (Exception e)
  • System.out.println("oops!")
  • System.out.println("goodbye")

9
Getting the message back
  • When you receive an exception
  • Get the message text with
  • Exception object . getmessage()
  • Example
  • catch (IllegalArgumentException e)
  • System.out.println("oops!"
    e.getMessage())

10
Checking Types
  • You can throw different types of exceptions
  • Test them in order using elseif logic
  • catch (IllegalArgumentException e)
  • System.out.println("oops!"
    e.getMessage())
  • catch(Exception e)
  • System.out.println("oops2 ")

11
Summary
  • You can stop executing a method with a throw
    command.
  • Create an exception to throw new
    IllegalArgumentException(tell about the error)
  • You can catch the error in the calling program
    using a try /catch block.
  • Try
  • Catch
Write a Comment
User Comments (0)
About PowerShow.com