public function() {
try {
// execute code and if error jump into 1st catch
return output;
} catch (Exception e){
//execute code and if error jump in 2nd catch
return output1
} catch (Exception e){
//execute code and stop
return output 2
}
}
The Above is the Code flow I want to achieve, I wanted to check if there is way or better way to do my scenario where I want to try a piece of code and if it fails, catch and execute first catch piece of code and if it again fails and exception occurs in first catch also go for final piece of code in last catch. Any help is very much appreciated. Thanks in advance.
Here is the flow I want to achieve :
try ----if error/exception----> catch1 ----if error/exception----> catch2
You can't do that. To catch a exception inside a catch block you should user another try/catch.
public function() {
try {
return output;
} catch (Exception e){
try {
return output1;
} catch (Exception e2) {
return output2;
}
}
}
Related
Basically i have 2 different commands i could possibly execute. If the first one does not work, I want to execute the second command.
Is there some easier, cleaner way to do this?
What would i even put in the if statement?
try
{
// code that may throw an exception
driver.findElement(By.id("component-unique-id-31")).click();
}
catch (Exception ex)
{
// Print to console
}
if(previous command threw an exception){
try
{
//Another command i want executed if the above fails
driver.findElement(By.xpath("//h3[normalize-space()='Something']")).click();
}
catch (Exception ex)
{
// handle
}
}
You can put the second command inside the catch block of the first try-catch, as following:
try
{
// code that may throw an exception
driver.findElement(By.id("component-unique-id-31")).click();
}
catch (Exception ex1)
{
try
{
//Another command i want executed if the above fails
driver.findElement(By.xpath("//h3[normalize-space()='Something']")).click();
}
catch (Exception ex2)
{
// handle
}
}
You can wrap up both the try-catch{} blocks within a single try-catch-finally{} block as follows:
try {
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("component-unique-id-31"))).click();
System.out.println("Within in try block.");
}
catch(TimeoutException e) {
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[normalize-space()='Something']"))).click();
System.out.println("Within in catch block.");
} finally {
System.out.println("The 'try catch' is finished.");
}
PS: You must avoid catching the raw exception.
I want to execute my method callmethod if the condition inside the IF statement is met. Else it should execute the catch block. But during implementation, if the condition is not met, it does not go to the catch block.
try{
if(count==0)
callmethod();
}
catch (Exception e){
System.out.println(e);
}
This is a good application for methods:
try {
if (count == 0) {
callOneMethod();
}
else {
callOtherMethod();
}
catch (Exception e) {
callOtherMethod();
}
That way you don't have any duplicated code and you're not doing weird things with exceptions in non-exceptional cases.
Since you are trying to hit the catch block, you need to throw an exception if your parameter is not met (i.e. count != 0).
Example:
try {
if(count==0){
callmethod();
} else {
throw new SomeException("some message");
}
}
catch (Exception e){
System.out.println(e);
}
How can i find the empty try catch blocks?
Using the Copy existing template... I found the structural search for try catch:
try {
$TryStatement$;
} catch($ExceptionType$ $Exception$) {
$CatchStatement$;
}
I want to enhance it so that it does only find try catches with empty catch blocks
It should find:
try {
assertTrue(output.validate());
} catch (Exception e) {
//TODO something
}
or
try {
assertTrue(output.validate());
} catch (Exception e) {
}
or
try {
assertTrue(output.validate());
} catch (Exception e) {}
However not:
try {
assertTrue(output.validate());
} catch (Exception e) {
e.printStackTrace();
}
Right now it obviously finds both since there's no differentiation betweens.
How can I add this extra check?
Use the template you have found and on CatchStatement variable set Min count and Max count to 0.
Not sure if this has already been answered, but.
I know that in java there is the try, catch and finally blocks, but is there one which is only called if try has no errors/exceptions?
Currently after stating the command that needs to be run, I'm setting a boolean to true, and after the try and catch block, the program checks for if the boolean is true.
I'm pretty sure that there is a much simpler way, help is appreciated!
Just put your code after the try...catch block and return in the catch:
boolean example() {
try {
//dostuff
} catch (Exception ex) {
return false;
}
return true;
}
This would also work if you put the return true at the end of the try block as the code would jump to the catch on error and not execute the rest of the try.
void example() {
try {
//do some stuff that may throw an exception
//do stuff that should only be done if no exception is thrown
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
No, there is no block called only if no exceptions were raised.
The catch block is called if there were exceptions, finally is called regardless.
As stated, you can emulate such a beast with something like:
bool completed = false;
try {
doSomeStuff();
completed = true;
} catch (Exception ex) {
handleException();
} finally {
regularFinallyHandling();
if (completed) {
thisIsTheThingYouWant();
}
}
but there's nothing built into the language itself that provides this functionality.
I have a problem understanding how the try{} catch(Exception e){...} works!
Let's say I have the following:
try
{
while(true)
{
coord = (Coordinate)is.readObject();//reading data from an input stream
}
}
catch(Exception e)
{
try{
is.close();
socket.close();
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
Section 2
try
{
is.close();
db.close();
}
catch(Exception e)
{
e.printStackTrace();
}
Let's say my while() loop throws an error because of an exception of is stream.
This will get me out of the infinite loop and throw me in the first catch(){............}
block.
My question is the following:
After throwing an exception, getting out of the loop while() and reaching to
catch(){
}
Will my program continue his execution and move on to section 2? As long as the exception was caught? Or everything ends in the first catch()?
I think you want to use finally after your first catch [catch (Exception e)] to close your streams:
try {
// Do foo with is and db
} catch (Exception e) {
// Do bar for exception handling
} finally {
try {
is.close();
db.close();
} catch (Exception e2) {
// gah!
}
}
As long as no exceptions are thrown in your catch clause, your program will continue execution after your catch (or finally) clause. If you need to rethrow the exception from the catch clause, use throw; or throw new Exception(ex). Do not use throw ex, as this will alter the stack trace property of your exception.
After the exception is caught, execution continues after the try/catch block. In this case, that's your section 2.
An uncaught exception will terminate the thread, which might terminate the process.
Yes, you are right. It will move to Section 2.
If you want your Section 2 bound to happen, irrespective of any exception generated, you might want to enclose them in a finally block.
try {
// Do foo with is and db
}
catch (Exception e) {
// Do bar for exception handling
}
// Bound to execute irrespective of exception generated or not ....
finally {
try {
is.close();
db.close();
}
catch (Exception e2) {
// Exception description ....
}
}