How to throw an exception in java

I am a C programmer and just learning some java recently because I am developing a single Android application. I am currently in a situation. The following is an example.

public Class ClassA{ public ClassA(); public void MyMethod(){ try{ //Some code here which can throw exceptions } catch(ExceptionType1 Excp1){ //Here I want to show one alert Dialog box for the exception occured for the user. //but I am not able to show dialog in this context. So I want to propagate it //to the caller of this method. } catch(ExceptionType2 Excp2){ //Here I want to show one alert Dialog box for the exception occured for the user. //but I am not able to show dialog in this context. So I want to propagate it //to the caller of this method. } } } 

Now I wanted to use the call to the MyMethod () method somewhere else in another class. If someone can provide me some piece of code, how to throw exceptions to the calling object MyMethod () so that I can display them in the calling method's dialog box.

Sorry if I'm not so clear and weird to ask this question.

+7
source share
4 answers

Just don't catch the exception in the first place and change the method declaration so that it can propagate them:

 public void myMethod() throws ExceptionType1, ExceptionType2 { // Some code here which can throw exceptions } 

If you need to take some action and then distribute it, you can change it:

 public void myMethod() throws ExceptionType1, ExceptionType2 { try { // Some code here which can throw exceptions } catch (ExceptionType1 e) { log(e); throw e; } } 

Here, ExceptionType2 is not caught at all - it will just propagate automatically. ExceptionType1 captured, logged, and then returned.

It is not recommended to have catch blocks that simply reduce the exception β€” unless there is some subtle reason (for example, to prevent the use of a more general catch block), you usually should simply remove the catch block.

+20
source

Do not catch it and do not collapse again. Just do it and catch it in the right place.

 public void myMethod() throws ExceptionType1, ExceptionType2 { // other code } 

Example

 public void someMethod() { try { myMethod(); } catch (ExceptionType1 ex) { // show your dialog } catch (ExceptionType2 ex) { // show your dialog } } 
+2
source

Just clear the exception

throw Excp1;

You will need to add an exception type to the MyMthod() declaration like this

 public void MyMethod() throws ExceptionType1, ExceptionType2 { try{ //Some code here which can throw exceptions } catch(ExceptionType1 Excp1){ throw Excp1; } catch(ExceptionType2 Excp2){ throw Excp2; } } 

Or simply omit try at all, since you no longer handle exceptions, unless you add extra code to the catch statements that do something with the exception before reconstructing it.

0
source

I always do it like this:

 public void MyMethod() throws Exception { //code here if(something is wrong) throw new Exception("Something wrong"); } 

then when you call the function

 try{ MyMethod(); }catch(Exception e){ System.out.println(e.getMessage()); } 
0
source

All Articles