What's Here?
- Members: 105,765
- Replies: 404,474
- Topics: 61,346
- Snippets: 2,214
- Tutorials: 597
- Total Online: 1,579
- Members: 63
- Guests: 1,516
Who's Online?
|
Welcome to Dream.In.Code |
|
|
Getting Java Help is Easy!
Join 105,765 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,579 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
|
in simple code, or Exceptions that need not be thrown, Try/Catch/Finally blocks can come in handy
|
Submitted By: William_Wilson
|
|
|
Rating:
|
|
Views: 2,580 |
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
|
|
|
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|