Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's different between throw, throws and try catch

 
jiayuangu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I get confused about the using of throw, throws and try catch,
when we use throw? when we use throws? when we use try catch?
please give me a example of these
Thanks
Jonathan
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan,
Your login name 'jiayuangu' does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Ajith
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<code>throw</code> is an action. It is a keyword used to raise an exception. In your program, you may chose to raise exceptions and catch them somewhere and in such situations you can use throw. For example, <code>throw new SubClassOfException();</code>
<code>throws</code> on the otherhand is a part of the declarative syntax of a method. When a method is declared/defined, as a part of the syntax you tell the compiler that the method might throw an exception. When the caller tries to call this method, the compiler ensures that the call is "safe" and is enclosed in a try-catch block and/or the calling method itself declares a throws clause for this exception.
<code>try-catch</code> is a mechanism using which you can deal with exceptions. Any method that throws a checked exception must be enclosed in a try-catch block and the exception thrown must be caught in the catch block. Optionally you can omit the try-catch block and declare the calling method itself with a throws clause that throws the uncaught exception. This rule is enforced by the compiler and there is no way to escape.
Hope that helps.
Ajith
 
ravi ckumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
throw is used to throw an exception explicitly by u, say throw new Exception();
throws is used to inform a caller of a method that this particular method might throw an exception, & the person calling it is supposed to use try catch or again thow it
try catch are the only mechanisms for actually catching the exception & doing something about it. throws just passes the buck to the caller of the method where as the try catch actually solve the prob of exception hope this helps !!
 
asheesh talwar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception mechanism is built around the throw & catch paradigm.
To throw an exception is to signal that an exceptional condition has occured.
To catch an exception is to take appropriate action to deal with exception.(An exception is caught by exception handler).
The exception need not be caught in the same context that it was thrown in.
Example 1.
public class DivisionByZero3 {
public void division() {
int num1 = 10;
int num2 = 0;
try {
System.out.println(num1 + " / " + num2 + " = " +(num1 / num2));
} catch (ArithmeticException e) {
System.out.println("Dealt with " + e);
}
System.out.println("Returning from division.");
}
public static void main(String args[]) {
new DivisionByZero3().division();
}
}

Example 2.

class DivisionByZeroException extends Exception {
DivisionByZeroException(String msg) { super(msg); }
}
public class DivisionByZero5 {
public void division() {
int num1 = 10;
int num2 = 0;
try {
if (num2 == 0) throw new DivisionByZeroException("/ by 0");
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
} catch (DivisionByZeroException e) {
System.out.println("Dealt with " + e);
}
}
public static void main(String args[]) {
new DivisionByZero5().division();
}
}

Example 3.
class DivisionByZeroException extends Exception {
public DivisionByZeroException(String msg) { super(msg); }
}
public class DivisionByZero6 {
public void division() throws DivisionByZeroException {
int num1 = 10;
int num2 = 0;
if (num2 == 0) throw new DivisionByZeroException("/ by 0");
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
System.out.println("Returning from division.");
}
public static void main(String args[]) {
try {
new DivisionByZero6().division();
} catch (DivisionByZeroException e) {
System.out.println("In main, dealt with " + e);
} } }

I hope these will clear some of ur mysteries about exception handling .Jonathan what I would suggest U is to go through any good book on exception handling.

Regards
Ashish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic