Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throw exception to next catch?

Tags:

enter image description here

I want to throw an exception at next catch, (I attached image)

Anybody know how to do this?

like image 838
Expert wanna be Avatar asked Nov 26 '12 21:11

Expert wanna be


People also ask

How do you throw an exception in catch block?

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects). Or, wrap it within a new exception and throw it.

What is Rethrowing an exception?

If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. The rethrow expression ( throw without assignment_expression) causes the originally thrown object to be rethrown.

Is it good practice to throw exception in catch block?

It's fine practice to throw in the catch block. It's questionable practice to do so ignoring the original exception.

Can I throw exception in Catch block C++?

In C++, inside catch block we can re-throw an exception using throw statement, but the thrown exception should have the same type as the current caught one.


2 Answers

C# 6.0 to the rescue!

try { } catch (Exception ex) when (tried < 5) { } 
like image 52
l33t Avatar answered Nov 05 '22 12:11

l33t


You can't, and trying to do so suggests that you've got too much logic in your catch blocks, or that you should refactor your method to only do one thing. If you can't redesign it, you'll have to nest your try blocks:

try {     try     {         ...     }     catch (Advantage.Data.Provider.AdsException)     {         if (...)         {             throw; // Throws to the *containing* catch block         }     } } catch (Exception e) {     ... } 
like image 34
Jon Skeet Avatar answered Nov 05 '22 11:11

Jon Skeet