What's Here?
- Members: 244,283
- Replies: 693,128
- Topics: 113,159
- Snippets: 3,863
- Tutorials: 935
- Total Online: 1,132
- Members: 60
- Guests: 1,072
|
in simple code, or Exceptions that need not be thrown, Try/Catch/Finally blocks can come in handy
|
Submitted By: William_Wilson
|
|
|
Rating:
|
|
Views: 3,138 |
Language: Java
|
|
Last Modified: May 5, 2006 |
Instructions: Try is the code which MAY generate an error.
Catch is the code to run upon an error in the Try block.
Finally is run no matter what after the Try block upon completion or after the Catch block in the case of an error.
A full class Exceptions is provided as an example of all 3 blocks. |
Snippet
public class Exceptions{
public static void main (String args []){
try{
System. out. println("Try Block before the error.");
System. out. println("Try Block after the error."); //this line will not print
}
catch(java.lang.ArithmeticException e){
System. out. println("Catch Block");
System. out. println("A Stack Trace of the Error:");
e.printStackTrace();
//e.getMessage(); //This one is useable when we write our own exceptions
System. out. println("The operation is not possible.");
}
finally{
System. out. println("Finally Block");
}
}
}
Copy & Paste
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|