+ 5
What are catches, try, Expectations, and 'e'
im so confused. i really dont get when to use the exception, 'Exception e' and catch. help D:
2 Answers
+ 9
try: try to execute your code
catch: catch exceptional event from the try block
in code:
try { code ...}
catch ( Exception e ){ code....}
+ 4
what is try block?
The try block contains a block of program statements within which an exception might occur. A try block is always followed by a catch block, which handles the exception that occurs in associated try block. A try block must followed by a Catch block or Finally block or both.
Syntax of try block
try{
//statements that may cause an exception
}
What is catch block?
A catch block must be associated with a try block. The corresponding catch block executes if an exception of a particular type occurs within the try block. For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception executes.
Syntax of try-catch block
try {
//statements that may cause an exception
} catch (exception(type) e(object))â {
//error handling code
}
Flow of try-catch block
If an exception occurs in try block then the control of execution is passed to the catch block from try block.
The exception is caught up by the corresponding catch block. A single try block can have multiple catch statements associated with it, but each catch block can be defined for only one exception class. The program can also contain nested  try-catch-finally block
After the execution of all the try blocks, the code inside the finally block executes. It is not mandatory to include a finally block at all, but if you do, it will run regardless of whether an exception was thrown and handled by the try and catch blocks.