110 likes | 141 Views
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){
E N D
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){ if (x < 0){ throw new IllegalArgumentException(x + “is negative and must be 0 or greater”);} else { do what you want the method to do;}
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.
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; } }
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
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"); }
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”
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"); }
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()); }
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 ");}
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{}