{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ioexception - Exceptions An Exception is an object that...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Exceptions An Exception is an object that describes an unusual or erroneous situation. -If an exception is ignored by the program, the program will terminate and produce an appropriate message. Example: -------------------------------------------------------------------------------------- public class DividingByZero { //----------------------------------------------------------------- // Deliberately divides by zero to produce an exception. //----------------------------------------------------------------- public static void main (String[] args) { int numerator = 10; int denominator = 0; System.out.println (numerator / denominator); System.out.println ("This text will not be printed."); } } ------------------------------------------------------------------------------------ Output Exception in thread “main” java.lang.ArithmeticException: / by zero at DividingByZero.main(DividingByZero.java:11) -------------------------------------------------------------------------------------- The call stack trace : indicating where the exception occurred. This exception was produced by the line “System.out.println (numerator / denominator);” because it divides by zero. At this point, the program is terminated and the next line is never executed.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
We can avoid the termination of a program caused by an exception by using: try statements. Try statements try { // the line that throws the exception is included here } catch ( //put the exception object produced in try , e.g., IOException ) { } finally { // the statements inside of finally
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern