EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CS102--Object Oriented Programming
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
1 Week 11 l Basic Exception Handling »the mechanics of exceptions l Defining and Using Exceptions »some "simple" cases l Reality Check »guidelines for.
Exception Handling Chapter 8. Outline Basic Exception Handling Defining Exception Classes Using Exception Classes.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling By: Thomas Fasciano. What is an Exception?  An error condition that occurs during the execution of a Java Program.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch Announcements l Project 6 now out. »Milestone due Oct. 24th »Final project.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exception Handling Recitation – 10/(23,24)/2008 CS 180 Department of Computer Science, Purdue University.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Java Software Solutions Foundations of Program Design Sixth Edition
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Chapter 8-Exception Handling/ Robust Programming.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Lecturer: Dr. AJ Bieszczad Chapter 8 COMP 150: Introduction to Object-Oriented Programming 8-1 l Basic Exception Handling »the mechanics of exceptions.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Chapter 10 – Exception Handling
Introduction to Exceptions in Java
Why exception handling in C++?
Introduction Exception handling Exception Handles errors
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Announcements/Reminders
Exception Handling Chapter 8 Basic Exception Handling
Exceptions & exception handling
Exceptions & exception handling
Fundamental Error Handling
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
Exceptions 10-May-19.
Exception Handling Chapter 8 Basic Exception Handling
Java Basics Exception Handling.
Exceptions 5-Jul-19.
Presentation transcript:

EXCEPTIONS There's an exception to every rule.

2 Introduction: Methods  The signature of a method includes  access control modifier  return type  name of the method  parameter list  There is also an implied 'contract' for what the method should do when used.  This contract can be formalized as pre/post conditions  What should happen if the method can't fulfill the contract? Perhaps the pre-conditions aren't met?

3 Introduction: Example of 'error'  For instance, assume that a function with the signature “public double squareRoot(double value)” is written.  What would the contract be in general?  What would you think the pre/post conditions to be?  What would the value of “root” be in the statement double root = squareRoot(-1.0);  If a method may fail to work as expected due to some error (pre-condition violated for example), a method should generate an exception.  An 'exception' should generally be understood as an 'error'. Exceptions are used to:  Separate error-free code from erroneous code  Force programmers to specify and know what errors might arise in their code

4 Overview of the Exception class  Exceptions are objects!  Exception is the root class of all exceptions  An exception object should generally specify the precise reason/kind of error that occurred  Some common predefined exceptions:  ArrayIndexOutOfBoundsException  IOException  NullPointerException  FileNotFoundException  Many pre-defined methods throw exceptions  the documentation or source code will tell you when  the exceptions thrown are also pre-defined

5 Exception terminology  Throwing an exception  An error has occurred and the method cannot continue ‘normally’.  An Exception object is created and an error-handling process begins.  The error handling process 'jumps' to some line of code that is meant to take care of the error.  Handling an exception  Code that is meant to take care of an error.  This is also called catching an exception.  Java Syntax  To create and exception, create an Exception object (usually a subclass)  To throw an exception, use the throws clause.  Place code that may cause an error in a try block  Exception handling code is placed in a catch block

6 try-throw-catch syntax try { if(some_error_occurs) { throw new Exception(“An error has occurred"); } } catch(Exception e) { }

7 try-throw-catch flow of control  Details of try block:  Statements execute until a throw statement is executed.  If a throw statement is executed in the try block all code in the try block is done (even if not yet executed) Code jumps to the catch block associated with the try block  Details of the catch block:  Executes only if an exception is thrown from the associated try block.  Normal execution resumes after the code in the catch block completes

8 User enters 321 Example 1A Checking for valid data int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

9 evaluates to true (an error is detected) Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

10 create an Exception object Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

11 throw the Exception object Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

12 catch the exception object Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

13 the exception object is named “e” in this block Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

14 execute the “error handling” code Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

15 continue normally after the catch block Example 1A int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

16 User enters 32 Example 1B Checking for valid data int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

17 evaluates to false (no error is detected) Example 1B int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

18 continue execution normally since the “throw” statement is not executed Example 1B int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

19 continue execution normally since the “throw” statement is not executed Example 1B int age; try { System.out.print(“Enter your age: “); age = Keyboard.readInt(); if(age 120) { throw new Exception(“Invalid age: “ + age); } System.out.println(“Your age is ” + age); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(“Done.”);

20 Example 2 int a[] = new int[2]; try { for(int i=0; i<4; i++) { a[i] = 0; } System.out.println(“Done with the try block”); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); } System.out.println(“Array a is initialized.”);

21 when i = 2 this causes an ArrayIndexOutOfBoundsException Example 2 int a[] = new int[2]; try { for(int i=0; i<4; i++) { a[i] = 0; } System.out.println(“Done with the try block”); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); } System.out.println(“Array a is initialized.”);

22 this catch block handles the exception Example 2 int a[] = new int[2]; try { for(int i=0; i<4; i++) { a[i] = 0; } System.out.println(“Done with the try block”); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); } System.out.println(“Array a is initialized.”);

23 Exception Class Hierarchy Checked (you must follow all of the rules) OtherThanRuntimeException Not Checked (you can ignore some of the rules) RuntimeException

24 Rolling your own Exception classes  Must be derived from some pre-defined exception class  Often the only methods you need to define are the constructors  Include a constructor that takes a String message argument  Include a default constructor public class YouEnteredWrongDataException extends Exception { public YouEnteredWrongDataException() { } public YouEnteredWrongDataException(String message) { super(message); } public class YouEnteredWrongDataException extends Exception { public YouEnteredWrongDataException() { } public YouEnteredWrongDataException(String message) { super(message); }

25 Example of User Defined Exceptions public int getAge() throws YouEnteredWrongDataException { int result = Keyboard.readInt(); if(result 100) throw new YouEnteredWrongDataException(); return result; } public int getAge() throws YouEnteredWrongDataException { int result = Keyboard.readInt(); if(result 100) throw new YouEnteredWrongDataException(); return result; } Methods can throw exceptions and not catch them. This is the most common (and usually the best) way to write code!

26 Catching an exception in a method that doesn’t throw it!  When defining a method you must include a throws -clause if your method could throw an exception.  This tells the caller that it must deal with this possible error  The caller may either defer or handle Defer : simply not handle it (put in throws-clause) Handle : write a try-catch block This tells other methods: "If you call me, you must handle any exceptions that I throw."

27 Deferring methodA throws MyException but doesn’t catch it. Must be declared in the throws clause. methodB, which calls methodA, doesn’t catch MyException exceptions. Must be declared in the throws clause. public void methodA() throws MyException { … } public void methodA() throws MyException { … } public void methodB() throws AnException { … methodA(); … } public void methodB() throws AnException { … methodA(); … }

28 Handling the exception methodA throws AnException but doesn’t catch it. Must be declared in the throws clause. methodB, which calls methodA, catches MyException exceptions: public void methodA() throws MyException { …… } public void methodA() throws MyException { …… } public void methodB() { … try { MethodA(); … } catch(MyException e) { } … } public void methodB() { … try { MethodA(); … } catch(MyException e) { } … }

29 Uncaught exceptions  If an exception is not caught in the method that throws it or any of its calling methods, either:  the program ends  the program becomes unstable  Your main method should NEVER have a throws clause! “RunTimeExceptions" do not need a catch block or throws - clause…you can break the rules since these exceptions are not checked.

30 Multiple exceptions and catch blocks in a method  Methods can throw more than one exception  Try blocks can have more than one catch block associated with them  The catch blocks immediately following the try block are searched in sequence for a catcher of the appropriate type  the first catch block that handles the exception is the only one that executes  Specific exceptions are typically derived from more general types  So put the catch blocks for the more specific exceptions early and the more general ones later

31 Multiple Catch Blocks l If the call to “someMethod” throws an exception then: »If the exception is of type MyException1 then execute only the first catch block »Else if the exception is of type MyException2 then execute only the second catch block »Else if the exception is of type MyException3 then execute only the third catch block l If the call to “someMethod” throws an exception then: »If the exception is of type MyException1 then execute only the first catch block »Else if the exception is of type MyException2 then execute only the second catch block »Else if the exception is of type MyException3 then execute only the third catch block try { … someMethod(); // this may throw an exception … } catch(MyException1 me1) { } catch(MyException2 me2) { } catch(MyException3 me3) { }

32 Multiple Catch Block Example Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Object Throwable Exception BadThing1ExceptionBadThing2ExceptionBadThing3Exception Assume the class hierarchy shown below

33 Multiple Catch Block Example Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Assume the class hierarchy shown below Object Throwable Exception BadThing1Exception BadThing2Exception BadThing3Exception

34 Multiple Catch Block Example Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Sample thing = new Sample(); try { System.out.println(“about to do something”); thing.doSomething(); // this method may throw an exception System.out.println(“finished doing something”); } catch(BadThing1Exception bt1e) { System.out.println(“Bad thing 1 occurred”); } catch(BadThing2Exception bt2e) { System.out.println(“Bad thing 2 occurred”); } catch(BadThing3Exception bt3e) { System.out.println(“Bad thing 3 occurred”); } System.out.println(“continuing on with the program…”); Assume the class hierarchy shown below Object Throwable Exception BadThing3Exception BadThing2Exception BadThing1Exception

35 Multiple Catch Block Example BufferedReader fin = null; try { System.out.println(“about to open a file”); // this method may throw an Exception fin = new BufferedReader(new FileReader(file)); System.out.println(“finished opening the file”); } catch(FileNotFoundException fnf) { System.out.println(“File “ + file + “ doesn’t exist”); } catch(IOException io) { System.out.println(“Error with file “ + file + “.”); } System.out.println(“continuing on with the program…”); BufferedReader fin = null; try { System.out.println(“about to open a file”); // this method may throw an Exception fin = new BufferedReader(new FileReader(file)); System.out.println(“finished opening the file”); } catch(FileNotFoundException fnf) { System.out.println(“File “ + file + “ doesn’t exist”); } catch(IOException io) { System.out.println(“Error with file “ + file + “.”); } System.out.println(“continuing on with the program…”); Assume the class hierarchy shown below Object Throwable Exception IOException FileNotFoundException

36 Example Program  Write a class that represents a rational number  a number which is expressed as the quotient of two integers.  Must throw exceptions when reasonable  Write a program that demonstrates the use of this class Rational number ADT What data? What operations? What pre-conditions to the operations? What errors could occur in these operations? Rational number ADT What data? What operations? What pre-conditions to the operations? What errors could occur in these operations?

37 Example Program Using Exceptions class DivideByZeroException extends Exception { DivideByZeroException(String msg) { super(msg); } DivideByZeroException() {} } class DivideByZeroException extends Exception { DivideByZeroException(String msg) { super(msg); } DivideByZeroException() {} }

38 Example Program Using Exceptions class Rational { private int numerator, denominator public Rational(int num, int denom) throws DivideByZeroException { if(denom == 0) { throw new DivideByZeroException("Constructor error: “ + num + "/“ + denom); } numerator = num; denominator = denom; } public Rational times(Rational f) { Rational result = null; try { result = new Fraction(numerator*f.numerator, denominator*f.denominator); } catch(DivideByZeroException e) { } return result; } … } class Rational { private int numerator, denominator public Rational(int num, int denom) throws DivideByZeroException { if(denom == 0) { throw new DivideByZeroException("Constructor error: “ + num + "/“ + denom); } numerator = num; denominator = denom; } public Rational times(Rational f) { Rational result = null; try { result = new Fraction(numerator*f.numerator, denominator*f.denominator); } catch(DivideByZeroException e) { } return result; } … }

39 Example Program Using Exceptions class Rational { private int numerator, denominator; public Rational divide(Fraction f) throws DivideByZeroException { Rational result = null; if(f.numerator == 0) { throw new DivideByZeroException("Can't divide " + this + " by " + f); } try { result = new Rational(numerator*f.denominator, denominator*f.numerator); } catch(DivideByZeroException e) { } return result; } public String toString() { return numerator + "/" + denominator; } … } class Rational { private int numerator, denominator; public Rational divide(Fraction f) throws DivideByZeroException { Rational result = null; if(f.numerator == 0) { throw new DivideByZeroException("Can't divide " + this + " by " + f); } try { result = new Rational(numerator*f.denominator, denominator*f.numerator); } catch(DivideByZeroException e) { } return result; } public String toString() { return numerator + "/" + denominator; } … }

40 Example Program Using Exceptions public class TestProgram { public static void main(String[] args) { // Demonstration of exception handling in client code Rational f1, f2, f3, f4; try { f1 = new Rational(3, 5); f2 = new Rational(1, 2); f3 = f1.divide(f2); f4 = f1.times(f2); System.out.println(f1+" divided by "+f2+" equals "+f3); System.out.println(f1+" times " +f2+" equals "+f4); } catch(DivideByZeroException dbz) { System.out.println("DivideByZeroException:"); System.out.println(dbz.getMessage()); } catch(Exception me) { System.out.println("Exception:"); System.out.println(me.getMessage()); } public class TestProgram { public static void main(String[] args) { // Demonstration of exception handling in client code Rational f1, f2, f3, f4; try { f1 = new Rational(3, 5); f2 = new Rational(1, 2); f3 = f1.divide(f2); f4 = f1.times(f2); System.out.println(f1+" divided by "+f2+" equals "+f3); System.out.println(f1+" times " +f2+" equals "+f4); } catch(DivideByZeroException dbz) { System.out.println("DivideByZeroException:"); System.out.println(dbz.getMessage()); } catch(Exception me) { System.out.println("Exception:"); System.out.println(me.getMessage()); } Try the following: 1.f1 = new Fraction(3,0); 2.f2 = new Fraction(0, 2); Try the following: 1.f1 = new Fraction(3,0); 2.f2 = new Fraction(0, 2);