Chained Exceptions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jromero
    New Member
    • Sep 2007
    • 16

    #1

    Chained Exceptions

    Hi,

    I have a problem with chained the exceptions. I know the try and catch should be in main method but in this case I have 2 methods and the main method needs to throws an exception
    The exception is called NumberFormatExc eption

    I need Help..

    Code[

    public class Hexnumbers{


    public static int conv1(String s) {
    String digits = "0123456789ABCD EF";
    s = s.toUpperCase() ;
    int val = 0;
    for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    int d = digits.indexOf( c);
    val += d*Math.pow(16, s.length()-1-i);
    }
    return val;


    }


    public static String conv2(int d) {
    String digits = "0123456789ABCD EF";
    if (d == 0) return "0";
    String hex = "";
    while (d > 0) {
    int digit = d % 16;
    hex = digits.charAt(d igit) + hex;
    d = d / 16;
    }
    return hex;
    }



    public static void main(String[] args) {


    int sum = 0;

    for (int i = 0; i < args.length; i++) {
    System.out.prin tln(args[i]);


    int decimal = conv1(args[i]);
    System.out.prin tln("Here is the decimal number = " + decimal);
    sum += decimal;

    String hex = conv2( decimal);
    System.out.prin tln("Here is the Hex number = " + hex);

    System.out.prin tln("Here is the sum of **Hex** + **Decimal** == " + sum);
    }




    }






    }

    ]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Jromero
    Hi,

    I have a problem with chained the exceptions. I know the try and catch should be in main method but in this case I have 2 methods and the main method needs to throws an exception
    The exception is called NumberFormatExc eption

    I need Help..

    Code[

    public class Hexnumbers{


    public static int conv1(String s) {
    String digits = "0123456789ABCD EF";
    s = s.toUpperCase() ;
    int val = 0;
    for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    int d = digits.indexOf( c);
    val += d*Math.pow(16, s.length()-1-i);
    }
    return val;


    }


    public static String conv2(int d) {
    String digits = "0123456789ABCD EF";
    if (d == 0) return "0";
    String hex = "";
    while (d > 0) {
    int digit = d % 16;
    hex = digits.charAt(d igit) + hex;
    d = d / 16;
    }
    return hex;
    }



    public static void main(String[] args) {


    int sum = 0;

    for (int i = 0; i < args.length; i++) {
    System.out.prin tln(args[i]);


    int decimal = conv1(args[i]);
    System.out.prin tln("Here is the decimal number = " + decimal);
    sum += decimal;

    String hex = conv2( decimal);
    System.out.prin tln("Here is the Hex number = " + hex);

    System.out.prin tln("Here is the sum of **Hex** + **Decimal** == " + sum);
    }




    }






    }

    ]
    1.) Use code tags when posting code.
    2.) Your convert methods are the ones that can throw the exceptions so they are the ones that must be declared as throws whatever execption. Your main should then handle those exceptions using try/catch.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Jromero
      Hi,

      I have a problem with chained the exceptions. I know the try and catch should be in main method
      That is not true; any block of code in any method can be a try ... catch block.
      As a rule of thumb you should catch a thrown exception as close to where it makes
      sense. It is not 'fair play' to have main() handle them all because it doesn't know
      what do to with them at that outer most level.

      kind regards,

      Jos

      Comment

      Working...