Add Exception Handler

I am trying to include exception handling in:

    public static int hexToDecimal(char ch)  {
       
        int temp=0;
        try{
           
            if (ch<'0' || ch>'F')
                throws new NumberFormatException("not a hex number.");
               
            if (ch>='A' && ch <='F')
                temp = 10+ch-'A';
            else
                temp = ch-'0';
       
        }catch{
           
           
        }
        return temp;
    }

for when the char is not hex.

The complete code is included below.

Thank you.
import java.util.Scanner;

public class Exercise13_6{

    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
            
        
        System.out.print("Enter a hex number: ");
        String hex=input.nextLine();
        
        System.out.println("The decimal value for hex number "
                + hex + " is " + hexToDecimal(hex.toUpperCase()));
    }
    
    public static int hexToDecimal(String hex) {
        int decimalValue=0;
        for(int i=0; i<hex.length();i++) {
            char hexChar=hex.charAt(i);
            decimalValue=decimalValue*16+hexToDecimal(hexChar);
        }
            return decimalValue;
    }
    
    public static int hexToDecimal(char ch)  {
        
        int temp=0;
        try{
            
            if (ch<'0' || ch>'F')
                throws new NumberFormatException("not a hex number.");
                
            if (ch>='A' && ch <='F')
                temp = 10+ch-'A';
            else
                temp = ch-'0';
        
        }catch{
            
            
        }
        return temp;
    }
}

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
should be not "throws..."  but "throw"
for_yanCommented:
"throws" is in method declaration

"throw" when you need to throw exception
for_yanCommented:


     }catch{


should be

   } catch(Exception ex) {

(or maybe catch(NumberFormatException ex) {

but not

} catch {
 without Exception parentheses

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

for_yanCommented:

compiling and throwing exception:
import java.util.Scanner;

public class Exercise13_6{

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        System.out.print("Enter a hex number: ");
        String hex=input.nextLine();

        System.out.println("The decimal value for hex number "
                + hex + " is " + hexToDecimal(hex.toUpperCase()));
    }

    public static int hexToDecimal(String hex) {
        int decimalValue=0;
        for(int i=0; i<hex.length();i++) {
            char hexChar=hex.charAt(i);
            decimalValue=decimalValue*16+hexToDecimal(hexChar);
        }
            return decimalValue;
    }

    public static int hexToDecimal(char ch)  {

        int temp=0;
        try{

            if (ch<'0' || ch>'F')
                throw new NumberFormatException("not a hex number.");

            if (ch>='A' && ch <='F')
                temp = 10+ch-'A';
            else
                temp = ch-'0';

        }catch (NumberFormatException ex){
                         ex.printStackTrace();

        }
        return temp;
    }
}

Open in new window



Enter a hex number: oy
java.lang.NumberFormatException: not a hex number.
	at Exercise13_6.hexToDecimal(Exercise13_6.java:32)
	at Exercise13_6.hexToDecimal(Exercise13_6.java:21)
	at Exercise13_6.main(Exercise13_6.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
java.lang.NumberFormatException: not a hex number.
	at Exercise13_6.hexToDecimal(Exercise13_6.java:32)
	at Exercise13_6.hexToDecimal(Exercise13_6.java:21)
	at Exercise13_6.main(Exercise13_6.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
The decimal value for hex number oy is 0

Open in new window

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you for the solution.

re:> The decimal value for hex number oy is 0

Is it possible not to print this message when there is an error?
for_yanCommented:

import java.util.Scanner;

public class Exercise13_6{

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        System.out.print("Enter a hex number: ");
        String hex=input.nextLine();
        boolean good = true;
        int num = -100000;
                           try{
        num =  hexToDecimal(hex.toUpperCase());
                           }catch(Exception ex){
                               System.out.println("error");
                            good = false;
                           }
                             if(good)
        System.out.println("The decimal value for hex number "
                + hex + " is " + num);
    }

    public static int hexToDecimal(String hex) throws NumberFormatException {
        int decimalValue=0;
        for(int i=0; i<hex.length();i++) {
            char hexChar=hex.charAt(i);
            decimalValue=decimalValue*16+hexToDecimal(hexChar);
        }
            return decimalValue;
    }

    public static int hexToDecimal(char ch) throws NumberFormatException {

        int temp=0;
        try{

            if (ch<'0' || ch>'F')
                throw new NumberFormatException("not a hex number.");

            if (ch>='A' && ch <='F')
                temp = 10+ch-'A';
            else
                temp = ch-'0';

        }catch (NumberFormatException ex){
                         throw ex;

        }
        return temp;
    }
}

Open in new window


Output:
Enter a hex number: 0y
error

Open in new window



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.