1 / 11

Introduction to Computer Programming

Introduction to Computer Programming. Error Handling. What to do with errors?. Return something the caller will understand: Public static int myMethod(int x){ if (x < 0){ return -1;} else { do what you want the method to do;} Throw Exception – Public static int myMethod(int x){

kramey
Download Presentation

Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 < 0){ return -1;} else { do what you want the method to do;} Throw Exception – Public static int myMethod(int x){ if (x < 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 < 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{}

More Related