Beginning Java for Xojo Programmers VII

exceptions

Classes

Upper case type names (Integer vs int, String vs string etc)

Beginning Java for Xojo Programmers VII

Exceptions
Xojo and Java have a reasonable amount in common when it comes to exceptions.
So they should be fairly familiar.
There is one major difference though.
Java uses “checked exceptions” meaning the code you write either has to deal
with the exceptions it can OR declare that it will throw such an exception.
There’s NO ambiguity about which ones code may or may not raise which is different
from Xojo where its not clearly stated what exceptions framework methods might raie etc.
In java what can be thrown as an exception is part of the declaration of a method.

In Java if you do something like



class Exceptions {

    public static void foo() {

        throw new Exception( "this is an exception") ;

    }
	public static void main(String[] args) 
	{
	
		System.out.println("Hello World" ) ;
		
       foo() ;

	}
	
}	

and try to run the Java compiler will balk at you

I’ve only been using VS Code for Java work for a very short while so I’m sure I’m not using it right here. but if you have this same code open in VS code and run it you get a very different result.
It runs but fails with the unhandled and uncaught exception when I really think it should not have compiled.

However, one very helpful thing it does give you is an accurate stack trace back to the source of the error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        Unhandled exception type Exception

        at Exceptions.foo(Exceptions.java:6)
        at Exceptions.main(Exceptions.java:14)

the last line, is where the foo() method was call in main.
And the one above that is the exact line of code that caused the exception to be raised.

And in the screen shot above yu can see theIDE has highlighted line 6 as a problem (which is nice)
If there were many you could navigate fro one to the next using Go > Next Problem or Go > Previous Problem
It makes working with a big java file a lot easier this way.

To fix one error we should change the decalaration of the foo method to say it throws an exception.
One thing you should NOT do is just shorthand everything and just say your method throws Exception, when it throws specific ones.
Thats not helpful to you or other coders in the long run
So alter the declaration to

   public static void foo() throws Exception {

Now if you compile at the command lline you see

Exceptions.java:14: error: unreported exception Exception; must be caught or declared to be thrown
       foo() ;
          ^
1 error

and the compiler is pointing to the exact line where the thrown exception is not being caught or dealt with otherwise.

VS code similarly points to the same line as a problem.

Screen Shot 4

So now what DO we want to do with this error ?
In Xojo we might wrap it int a try catch, which is harder since we arent quite sure that exceptions might be thrown to know which ones we could handle and which we cant.
In a Xojo console app our little Java app in this chapter might look like

	Event App.Open
		print("Hello World" ) 
		
        foo() 
	End Event
	
    Sub foo()
		raise new RuntimeException( "this is an exception") 
	End Sub

and in there IF we wanted to handle the event we would alter things to

	Event App.Open
		print("Hello World" ) 
		
        try
        	foo() 
        catch ex as RuntimeException
        	    print("Hello World caught the exception" ) 
        end try
        
	End Event
	
    Sub foo()
		raise new RuntimeException( "this is an exception") 
	End Sub

Java is not that much different

class Exceptions {

    public static void foo() throws Exception {

        throw new Exception( "this is an exception") ;

    }
	public static void main(String[] args) 
	{
	
		System.out.println("Hello World" ) ;
		
        try {
            foo() ;
        } catch (Exception e) {
            //TODO: handle exception
            System.out.println("Hello World caught the exception" ) ;
        }

	}
	
}	

The biggest differences are that java of course surrounds everythin g that should be run as part of the try with { }
And that the catch portion looks more like a method declaration than anything. As well each catch uses { } to enclose all the code that should be run a part of that catch.

But its pretty easy to see how to move Java to Xojo or vide versa here.

1 Like