1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error=0.000001; double x0.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Yoshi
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Index Exception handling Exception In Java Exception Types
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
1. 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
1 Nested Classes O. 2 Possible to declare a class within another class; called nested classes Nested class should have some specific association with.
Java Computer Industry Lab. 1 Programming Java Exception Handling Incheon Paik.
Exceptions CIS 304 Intermediate Java Programming for Business.
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.
Preventing and Correcting Errors
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
EXCEPTION HANDLING.
Object Oriented Programming with Java (150704).  Throwable Exception (This class will catch exceptions generated by prog.) (Create your own custom exception.
Slides Credit Umair Javed LUMS Web Application Development.
Chapter 9: Exceptions For error/problem situations Exception classes –ArithmeticException, IOException, etc. –checked exceptions try blocks –catch statements.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
CSE 1201 Object Oriented Programming
Chapter 10 – Exception Handling
MIT AITI 2003 Lecture14 Exceptions
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
COMPSCI 230 S Programming Techniques
ATS Application Programming: Java Programming
TRY CATCH BLOCK By Kosala Rajapaksha.
Exception Handling in Java
Web Design & Development Lecture 7
Exception Handling in Java
Managing Errors and Exceptions
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Presentation transcript:

1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error= ; double x0 = 3.0; double c = 5.0; double x1 = x0 - (x0 * x0 * x0 - c) / (3 * x0 * x0); while(Math.abs(x1-x0)>error){ x0=x1; x1 = x0 - (x0 * x0 * x0 - c) / (3 * x0 * x0); } System.out.println(x1); }

2 Newton-Raphson /* Solve f(x) = x*x-c = 0 f'(x) = 2x */ public class newton{ public static void main(String args[]){ double error= ; double x0 = 3.0; double c = 5.0; double x1 = x0 - (x0 * x0 - c) / (2 * x0); while(Math.abs(x1-x0)>error){ System.out.println(x1); x0=x1; x1 = x0 - (x0 * x0 - c) / (2 * x0); } System.out.println(x1); }

3 Exception JVM can generate an exception in response to some internal error. Your program cannot handle this. Standard exceptions (divide by zero, array-index out of bounds) You can manually generate an exception by using the throw statement

4 Exception try block contains program statements that you want to monitor. If an exception occurs within try block it is thrown. Your code can catch this exception using catch and handle it in some rational manner. Use the keyword throw to manually throw an exception. In some cases, an exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed upon exiting from a try block is put in a finally block.

5 exceptions // Let JVM handle the error. class NotHandled { public static void main(String args[]) { int nums[] = new int[4]; System.out.println("Before exception is generated."); // generate an index out-of-bounds exception nums[7] = 10; } Before exception is generated. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at ExcDemo1.main(java.lang.String[]) (Unknown Source) at gnu.java.lang.MainThread.call_main() (/usr/lib/libgcj.so.6.0.0) at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)

6 exceptions // Demonstrate exception handling. class ExcDemo1 { public static void main(String args[]) { int nums[] = new int[4]; try { System.out.println("Before exception is generated."); // Generate an index out-of-bounds exception. nums[7] = 10; System.out.println("this won't be displayed"); } catch (ArrayIndexOutOfBoundsException exc) { // catch the exception System.out.println("Index out-of-bounds!"); //System.out.println(exc); } System.out.println("After catch statement."); } Before exception is generated. Index out-of-bounds! After catch statement.

7 exceptions /* An exception can be generated by one method and caught by another. */ class ExcTest { // Generate an exception. static void genException() { int nums[] = new int[4]; System.out.println("Before exception is generated."); // generate an index out-of-bounds exception nums[7] = 10; System.out.println("this won't be displayed"); } class ExcDemo2 { public static void main(String args[]) { try { ExcTest.genException(); } catch (ArrayIndexOutOfBoundsException exc) { // catch the exception System.out.println("Index out-of-bounds!"); } System.out.println("After catch statement."); }

8 exceptions // Handle error gracefully and continue. class ExcDemo3 { public static void main(String args[]) { int numer[] = { 4, 8, 16, 32, 64, 128 }; int denom[] = { 2, 0, 4, 4, 0, 8 }; for(int i=0; i<numer.length; i++) { try { System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); } catch (ArithmeticException exc) { // catch the exception System.out.println("Can't divide by Zero!"); } 4 / 2 is 2 Can't divide by Zero! 16 / 4 is 4 32 / 4 is 8 Can't divide by Zero! 128 / 8 is 16

9 exceptions // Use multiple catch statements. class ExcDemo4 { public static void main(String args[]) { // Here, numer is longer than denom. int numer[] = { 4, 8, 16, 32, 64, 128, 256, 512 }; int denom[] = { 2, 0, 4, 4, 0, 8 }; for(int i=0; i<numer.length; i++) { try { System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); } catch (ArithmeticException exc) { // catch the exception System.out.println("Can't divide by Zero!"); } catch (ArrayIndexOutOfBoundsException exc) { // catch the exception System.out.println("No matching element found."); } 4 / 2 is 2 Can't divide by Zero! 16 / 4 is 4 32 / 4 is 8 Can't divide by Zero! 128 / 8 is 16 No matching element found.