Javascript  
 
  ScriptJava  
 
  Perl  
 
  PHP  
 
  ROR  
 
  ask us  
 

 

Applications
  1. Getting started
  2. Definitions
  3. First program
  4. Characters and Strings
  5. Object and methods
  6. Arrays and Circles
  7. Exceptions
  8. The main method
  9. Reading
  10. Writing file
  11. Vectors
  12. Stacks
  13. Map The Dictionary
  14. Lists
  15. Linked lists
  16. Collection
  17. Interfaces
  18. Scanner
  19. StringTokenizer
  20. Generics
  21. JDBC
  22. DataBase Queries
  23. JSP, The main step

Graphics

Applets
  1. Regards
  2. One Picture
  3. Calculator
  4. Random pictures
  5. Bouncing picture

Swings
  1. Buttons listeners
  2. TextFields
  3. Swing Example

JavaBeans
  1. The first step
  2. Example

Search a word:
   



© The scientific sentence. 2010

Exceptions
Exceptions    Main clauses: throw, throws, try, catch, finally

I. Introduction:

/** An exception is a case where the programm gets disturbed and cannot continue to run; for example, when computing and falls in the circumstance to compute a division by zero. We want to wrap this event in Exception clause et allow the program to rum. We often encounter Exceptions like IOException (I/O). SQLException (Database access), EOFException (End Of File), ServletExcepton, ArithmeticException, etc ... The Exception method has three clauses: try, catch, and finally. In the first, we put a related code; in the second what to do in the case of the disturbance, and in the third, other statements. We hide the result of an exception by throw or throws. ArthmeticException stems from RuntimeException after Exception , Throwable, and Object classes. Here are some examples: */

I. Examples: 

class TheExceptions extends ArithmeticException{
int x ;
int y ;

static void TheDivision1  (int x, int y){
//The first method : Will give the result of the disvision of x by y; 
//and gives an exception if y = 0.
//if (y == 0) System.out.println("The result of x/y is: infinity");
//else
System.out.println("\n\t The result of x/y is: " + x/y);
}


static void TheDivision2  (int x, int y){
//The second method : Will give the result of the disvision of x by y; 
//and will mention the specified  exception if y = 0.
if (y == 0) System.out.println("\n\t The result of x/y is: infinity");
else
System.out.println("\n\t The result of x/y is: " + x/y);
}


static void TheDivision3  (int x, int y){
//The third method : Will give the result of the disvision of x by y; 
//and will mention the specified  exception if y = 0.
try {System.out.println("\n\t The result of x/y is: " + x/y);}
catch (ArithmeticException e){ System.out.println("\n\t The exception 
is catched.");}
}


static void TheDivision4  (int x, int y){
//The fourth method : Will give the result of the disvision of x by y; 
//and will mention the specified  exception if y = 0, 
// and gives a message by using the getMessage(0  prdefined method.
try {System.out.println("\n\t The result of x/y is: " + x/y);}
catch (ArithmeticException e){ System.out.println("\n\t The exception is 
catched, because of the " + e.getMessage());}
}



static void TheDivision5  (int x, int y) throws ArithmeticException {
try {System.out.println("\n\t The result of x/y is: " + x/y);}
catch (ArithmeticException e){ System.out.println("\n\t The exception 
¸is catched, because of the " + e.getMessage());}
finally { if(x == 0 && y == 0) System.out.println("\n\t There is 
an indetermination");}
}
/*
The two exceptions here is hidden; The first dividing by zero (/0)is 
cathced, and  the second is 
an indetermination (0/0) is cached by if condition.
*/
static void TheDivision6  (int x, int y) throws ArithmeticException 
{// Does not catch the exception alone.
System.out.println("\n\t The result of x/y is: " + x/y);
}

static int TheDivision7  (int x, int y) throws ArithmeticException {
if( y == 0 ) throw new ArithmeticException ("\n\t No exception (/by 
zero) is rised because the arithmeticException is thrown.");
return x/y;
}


public static void main (String [] args) 
{
System.out.println("\n\t1. No exception is used:");
	TheDivision1  (15,3);
	//TheDivision1  (15,0);
	
/*The output is :
The result of x/y is: 5
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at TheExceptions.TheDivision1(TheExceptions.java:9)
        at TheExceptions.main(TheExceptions.java:15)
*/	
	
System.out.println("\n\t2. The exception is blocked by if condition:");
	TheDivision2  (15,3);		
	TheDivision2  (15,0);
	
/* Comment TheDivision1; the output is:
The result of x/y is: 5
The result of x/y is: infinity
*/

System.out.println("\n\t3. The exception is catched:");
	TheDivision3  (15,3);		
	TheDivision3  (15,0);
	
/* Comment TheDivision1; the output is::
  The result of x/y is: 5
  The exception is catched.
*/


System.out.println("\n\t4. The exception is catched with a 
related message:");
	TheDivision4 (15,3);		
	TheDivision4  (15,0);

	
/* Comment TheDivision1; the output is::
 The result of x/y is: 5
 The exception is catched, because of / by zero
*/


System.out.println("\n\t5. The exception is thrown, the 
clause finally is used");
	TheDivision5 (66,3);		
	TheDivision5  (0,0);

	
	System.out.println("\n\t6. The Exception is thrown with throws: ");
	TheDivision6 (15,3);		
	//TheDivision6  (5,0); //Nothing is thrown.

	System.out.println("\n\t7. The exception is thrown, but: ... ");
	System.out.println("\n\t The result of x/y is: " + TheDivision7 (550,11));

	int z = 333;
	try {z = TheDivision7 (77,0);}
	catch (ArithmeticException e){}
	System.out.println("\n\t... but 77/0 = " + z);

}//End of main method

}//End oc class TheExceptions



/* Execution:
C:\Java> java TheExceptions
        1. No exception is used:
         The result of x/y is: 5
        2. The exception is blocked by if condition:
         The result of x/y is: 5
         The result of x/y is: infinity
        3. The exception is catched:
         The result of x/y is: 5
         The exception is catched.
        4. The exception is catched with a related message:
         The result of x/y is: 5
         The exception is catched, because of the / by zero
        5. The exception is thrown, the clause finally is used
         The result of x/y is: 22
         The exception is catched, because of the / by zero
         There is an indetermination
        6. The Exception is thrown with throws:
         The result of x/y is: 5
        7. The exception is thrown, but: ...
         The result of x/y is: 50
        ... but 77/0 = 333
C:\Java>
*/						

 The main sytax:
modifier type method (arguments) throws X-Exception{
try{code ... ;}
catch (X-Exception e) { e.getmessage();}
finally {other things;}
}
or:
modifier type method (arguments) throws X-Exception{
code ... ;
throw new X-Exception ();
}


  
Google
Web
ScientificSentence
 




chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2009. All rights reserved.