Java Reference

Java Reference Home | Smart Soft Home

try

Description

try is used to identify a statement or block of statements that may fail (throw an exception). A try code block is always imediately followed by a catch block. The catch block is used to handle the exception. The catch block always takes one argument of type Exception (Or an Exception subclass).

Example

try{
  s = "X";
  i = Integer.parseInt(s);
}
catch(NumberFormatException ex){
  System.out.println(s + " can't be converted to an int");
  return;
}