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?
 
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



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

"throw" when you need to throw exception
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
for_yanCommented:


     }catch{


should be

   } catch(Exception ex) {

(or maybe catch(NumberFormatException ex) {

but not

} catch {
 without Exception parentheses

0
 
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

0
 
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.